Spring将URL映射到控制器和视图

时间:2015-06-02 18:43:08

标签: java spring request-mapping

我在Netbeans 8.0.2中有标准的Spring 4.x文件。我在RegisterController.java - 包中添加了一个控制器controllers。我还添加了一个模型User,其中包含有关用户的一些基本信息。接下来,我制作了2个.jsp个文件,Registration.jspRegistrationSuccess.jsp

这是我的 RegisterController

@Controller
@RequestMapping(value="/register")
public class RegisterController {
    @RequestMapping(method=RequestMethod.GET)
    public String viewRegistration(Model model){
        User user = new User();
        model.addAttribute("userForm", user);

        List<String> professionList = new ArrayList<>();
        professionList.add("Developer");
        professionList.add("Designer");
        professionList.add("IT Manager");
        model.addAttribute("professionList", professionList);

        return "Registration";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> Model){
        System.out.println("username: " + user.getUsername());
        System.out.println("password: " + user.getPassword());
        System.out.println("email: " + user.getEmail());
        System.out.println("birth date: " + user.getBirthDate());
        System.out.println("profession: " + user.getProfession());

        return "RegistrationSuccess";
    }
}

现在,转到myProject/register会产生404.我很困惑Spring如何管理路由。有一个 web.xml ,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

认为这意味着每个*.htm的网址都会转到 dispatcher-servlet

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

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

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

但是我需要在哪里插入一些条目让我的RegisterController工作?

3 个答案:

答案 0 :(得分:3)

由于您在url模式中提供了* .htm,因此dispatcher-servlet将只识别* .htm请求。 更改您的web.xml

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping> 

在您的控制器上方,无需提供RequestMapping。您的方法将为您完成工作

@Controller
public class RegisterController {

在您的方法之上,您应该给出值,以便您的方法可以找到它

@RequestMapping(value="/register.htm" method=RequestMethod.GET)
public String viewRegistration(Model model)

答案 1 :(得分:0)

根据Dispatcher Servlet映射,您的RequestMapping将使用扩展名为.htm的HttpRequest转到控制器。在您的情况下,您已经提到* .htm

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
</servlet-mapping>

所以在控制器中,对于相应的请求,添加如下..

@RequestMapping(value="/register.htm" method=RequestMethod.GET)
    public String viewRegistration(Model model){

并在控制器的顶部,添加如下,

@Controller
@RequestMapping("/")
public class RegisterController {

在spring.xml中添加它,

<context:component-scan base-package="RegisterController" />

包括Controller的包名称应该在component-scan base-package ...

答案 2 :(得分:0)

web.xml中的servlet映射编辑为:

 <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
 </servlet-mapping>

并在spring配置文件中添加以下代码:

<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="your.packagename.RegisterController"></context:component-scan>