开发者

Newbie in Spring MVC

I am a newbie in doing spring MVC. I was able to create a helloSpring sort of application but this was based on annotations. I removed annotations since I am still learning Spring MVC so I wanted to first learn XML config way but for some reason my jsp does not render once I remove the annotations part. I think I have all configs set correctly. Oddly my JSP modification does not even render on the page when it comes up so I hit enter on the address bar to confirm if Tomcat is not rendering a cached setting and it fails with 404. I am using STS as my IDE.

web.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" />

Homecontroller

    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;  }

}

home.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>

index.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" />

Error in Tomcat console.

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

I also changed the Controller code just now to ..

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

Thanks Dhiren


There doesn't seem to be any sort of error, just that no controller and action is being mapped to with that URL. Based on what you have posted, there are no routes defined so Spring MVC doesn't know what to do.

I would suggest that you turn up the logging to see exactly what spring is doing, which will give you a much better idea of where in the pipeline things are failing.

I too recently learned spring, and with Spring MVC 3.0, annotations are the way to go with the controllers, since it is so much easier and less error-prone than trying to get everything hooked up correctly in an XML config.

If you are concerned that using annotations couples your controllers to Spring, there are so many other Spring pieces that are needed in real controllers that you won't really be able to avoid it anyways without substantial effort. Since controllers should be thin anyways, just allow your controllers (and front end, essentially) be tied to spring, and then you can keep your back-end as decoupled as you like.


I'm not sure you can provide a wildcard in the name of the controller during the bean definition:

<beans:bean name="/*.htm" class="com.test.project.HomeController" ></beans:bean>

You could move the mapping to a org.springframework.web.servlet.handler.SimpleUrlHandlerMapping instance.

In general though, like @cdeszaq says, as of Spring 3.0 annotations are the preferred means of creating MVC objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜