java - Spring MVC 新手

标签 java spring model-view-controller spring-mvc

我是做spring MVC的新手。 我能够创建一个 helloSpring 类型的应用程序,但这是基于注释的。 我删除了注释,因为我仍在学习 Spring MVC,所以我想首先学习 XML 配置方式,但由于某种原因,一旦我删除了注释部分,我的 jsp 就不会呈现。我想我已经正确设置了所有配置。奇怪的是,我的 JSP 修改出现时甚至没有呈现在页面上,所以我在地址栏上按下回车键以确认 Tomcat 是否没有呈现缓存设置并且它失败并显示 404。我使用 STS 作为我的 IDE。

网络.xml

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

servlet-config.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<!--  annotation-driven /-->

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/jsp/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean name="/*.htm" class="com.test.project.HomeController" ></beans:bean>
<context:component-scan base-package="com.test.project" />

家庭 Controller

    protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub
        //logger.info("Welcome home! the client locale is "+ locale.toString());

        java.util.Date today = Calendar.getInstance().getTime();
        ModelAndView mv = new ModelAndView("home");
         mv.setViewName("home");
        mv.addObject("today", today);
        return mv;  }

}

首页.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home- My version</title>
</head>
<body>
<h1>
    Hello world!<b> Myversion- version-1</b>  
</h1>

<P>  The time on the server is ${serverTime2}. </P>
</body>
</html>

索引.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <c:redirect url="/home.htm" />

Tomcat 控制台出错。

Aug 28, 2011 9:44:57 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_24\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.6.0_24\jre\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\EgisTec MyWinLocker\x86;C:\Program Files (x86)\EgisTec MyWinLocker\x64;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\GTK2-Runtime\bin
Aug 28, 2011 9:44:57 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TestSpring' did not find a matching property.
Aug 28, 2011 9:44:58 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 28, 2011 9:44:58 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 28, 2011 9:44:58 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 939 ms
Aug 28, 2011 9:44:58 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 28, 2011 9:44:58 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.16
Aug 28, 2011 9:45:00 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 28, 2011 9:45:00 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 28, 2011 9:45:00 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2074 ms

刚才的Controller代码我也改成..

     mv.setViewName("home");
    mv.addObject("serverTime2", today);

谢谢 迪仁

最佳答案

似乎没有任何类型的错误,只是没有 Controller 和操作被映射到该 URL。根据您发布的内容,没有定义路由,因此 Spring MVC 不知道该怎么做。

我建议您打开日志记录以准确查看 spring 正在做什么,这将使您更好地了解管道中哪里出现故障。

我最近也学习了 spring,在 Spring MVC 3.0 中,注释是与 Controller 一起使用的方式,因为它比尝试在 XML 配置中正确连接所有内容更容易且更不容易出错。

如果您担心使用注解会将您的 Controller 耦合到 Spring,那么在实际 Controller 中还需要很多其他 Spring 组件,如果不付出大量努力,您将无法真正避免它。由于无论如何 Controller 都应该很薄,只需让您的 Controller (本质上是前端)绑定(bind)到 spring,然后您就可以根据需要保持后端解耦。

关于java - Spring MVC 新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7238148/

相关文章:

java - 如何在Spring Boot中从实体类调用基于服务的类

java - 负数的立方根

java - 如何在数据点上添加点击监听器?

java - Spring:java.io.NotSerializedException:MapSqlParameterSource

java - 考虑定义一个类型为 'org.flywaydb.core.Flyway' 的 bean

c++ - 数据数组返回问题,覆盖

javascript - 如何在 ASP.NET MVC 中将数据从 React 发送到 Controller?

java - 如何获取 Web 服务器中的 servlet 参数列表?

java - 归约化约简输出的键值对

ios - 在 MVC 中绘制模型 View 或 Controller 的 UIBezierPath 部分