Spring MVC 3.0映射扩展

dan*_*nik 5 mapping spring spring-mvc

我想将我的Spring MVC应用程序映射到jsp的以下扩展名*html和控制器的*.action.

这是登录页面:

@RequestMapping(value="/login.html", method=RequestMethod.GET)
public ModelAndView login(){
    ModelAndView mv = new ModelAndView("login/login");
    //omitted
    return mv;
}
Run Code Online (Sandbox Code Playgroud)

这是为了行动:

@RequestMapping(value="/login.action", method=RequestMethod.POST)
    public ModelAndView actionSignUp(@Valid User user){ 

    //omitted
    return mv;
    }
Run Code Online (Sandbox Code Playgroud)

和jsp很简单:

<form:form  method="POST" action="/login.action" commandName="user" >
<form:input path="userName" id="userName" /><br />
<form:input path="password" id="password"/><br />
 <input id ="login" type="submit" value="Login"></input>
</form:form>
Run Code Online (Sandbox Code Playgroud)

我的web.xml如下:

 <display-name>SpringMVC</display-name>
    <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
   <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
Run Code Online (Sandbox Code Playgroud)

获取/login.html没问题,但我在/login.action上遇到POST请求问题

我越来越

HTTP状态404 - /login.action

为什么我的*.action映射不起作用?

我可以映射两个不同的扩展名*html和*.action吗?

gou*_*uki 1

您表单中的操作方法定义为action="/login.action"。/ 使 url 与您的域相关。您应该注意到,表单发布到类似于http://localhost:8080/login.action的 url ,应该是 http://localhost:8080/[context root]/[sub dir if Defined]/login.action

我认为删除“/”可以修复它。