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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30604072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:22:03  来源:igfitidea点击:

Spring Mapping Url to Controller and View

javaspringrequest-mapping

提问by jdepypere

I have the standard Spring 4.x files in Netbeans 8.0.2. I added a controller, RegisterController.javain the controllers-package. I also added one model, a User, with some basic information about the user. Next I made 2 .jspfiles, Registration.jspand RegistrationSuccess.jsp.

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

Here is my RegisterController:

这是我的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";
    }
}

Now, going to myProject/registerresults in a 404. I am confused how Spring manages the routing though. There is a web.xmllooking like this:

现在,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>

What I thinkthis means is every url with *.htmgoes to the dispatcher-servlet:

认为这意味着每个 url*.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>

But where do I need to insert some entries to let my RegisterController do work?

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

回答by Arnab Dhar

Since you gave *.htm in url pattern, the dispatcher-servlet will only recognize *.htm requests. Change your web.xml

由于您在 url 模式中提供了 *.htm,调度程序 servlet 将只识别 *.htm 请求。更改您的 web.xml

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

And above your controller, there's no need to give RequestMapping. Your methods will do the work for you

在你的控制器之上,没有必要给RequestMapping. 您的方法将为您完成工作

@Controller
public class RegisterController {

And above your method, you should give value so that your method can find it

在你的方法之上,你应该赋予价值,以便你的方法可以找到它

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

回答by Vignesh Shiv

According to Dispatcher Servlet mapping, your RequestMapping will go to the controller with HttpRequest with .htm extension.. In your case, you have mentioned *.htm

根据 Dispatcher Servlet 映射,您的 RequestMapping 将转到带有 HttpRequest 扩展名的控制器。在您的情况下,您提到了 *.htm

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

So in the controller, for the appropriate request, add like this..

所以在控制器中,对于适当的请求,像这样添加..

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

and In the top of the controller, add like this,

并在控制器的顶部,像这样添加,

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

add this in spring.xml,

在 spring.xml 中添加这个,

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

including package name of Controller should be there in component-scan base-package...

包括控制器的包名应该在组件扫描基础包中...

回答by Arpit Aggarwal

Edit servlet mapping in you web.xmlas:

将您的 servlet 映射编辑web.xml为:

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

And also add below piece of code in your spring configuration file:

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

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