开发者

problem with very first spring project

I just switched from Strip开发者_如何转开发ed to Spring but i'm having issues with my very first project, Basically i get the 404 from the server. Strange enough, i have followed one by one all the steps in my book. I use Eclipse, Tomcat 6 and Spring 2.5 The structure of my project is like so: src> controllers(package)>SpringTestController(implements controller).....then ......web content>jsp(folder)>hello.jsp.....then....web-content>web-inf>SpringTest-servlet.xml and web.xml inside lib i have the 9 necessary jars.

my controller:

public class SpringTestController implements Controller{

public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {




    return new ModelAndView("jsp/hello.jsp");
}

}

my SpringTest-servet.xml

  <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean name="/hello.htm" class="controllers.SpringTestController"/>

</beans>

my web.xml(without header to save space)

<servlet>

        <servlet-name>SpringTest</servlet-name>

        <servlet-class>

          org.springframework.web.servlet.DispatcherServlet

        </servlet-class>

        <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

        <servlet-name>SpringTest</servlet-name>

        <url-pattern>*.htm</url-pattern>

  </servlet-mapping>

Where do you think the problem could be? I have been trying to look around the files but beside web.xml "where i dont see any abnormality" i'm very new to this flow structure so i really cannot get where the problem is.

Thx for your time


I'm suspicious of the practice of using the name attribute of bean to specify URL paths - while I'm sure it's probably possible, my answer will tell you how to do it using more traditional means.

First of all, here is the new SpringTest-servlet.xml:

<bean id="helloController" class="controllers.SpringTestController" />

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/hello.htm">helloController</prop>
        </props>
    </property>
</bean> 

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>   
</bean>

You'll probably notice a few things. I'm using the id attribute of bean to define your controller so it can be referenced elsewhere (in the urlMapping bean as you're about to see).

I define a urlMapping bean, which does exactly what you think it does - maps requests (e.g. /hello.htm) to a controller bean.

I've also used viewResolver to map views names to view files, however that is a personal preference thing. Since I'm now using a view resolver, your controller looks like this:

public class SpringTestController implements Controller {
    @Override
    public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        return new ModelAndView("hello");
    }
}

I don't need to put the path to the view because the viewResolver prepends it with "/WEB-INF/jsp/" and adds ".jsp" to the end. You can change the prefix to wherever you store your view files, or you can not use it at all. It's a personal preference thing, though I like to use it :)

Sorry if this answer didn't conform to your style - I tried to get it working your way and couldn't, so this is how I normally set up a Spring project (if I'm not using annotations).

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜