2013-06-02 27 views
0

我有一個視圖'hi.jsp'與用戶名和密碼文本字段。我需要提交'hi.jsp''LoginController.java'。如果提交的數據中有任何錯誤,則'LoginController.java'必須將請求重定向回'hi.jsp'(使用縮進輸入數據的文本字段)和相應的錯誤消息。更改數據並重新提交'hi.jsp'後,我收到404錯誤。Spring 3 MVC額外@RequestMapping值正在追加到返回的視圖

因此,第一次提交是成功的,但在第二次提交期間發生問題。 文件的源代碼被提及以下:

hi.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="s" %> 
    <%@ page session="false" %> 
    <html> 
     <body>  
     <s:form method="POST" modelAttribute="loginObj" action="login/validatelogin"> 
      <label for="userName">UserName</label> 
      <s:input path="userName" id="userName" size="15"/><br> 
      <div style="{color:red}"> <s:errors path="userName"></s:errors></div> 

      <label for="password">Password</label> 
      <s:input path="password" id="password" size="15" /><br> 
      <s:errors path="password"></s:errors> 

      <input name="submit" type="submit" value="login"/>  
    </s:form> 
    </body> 
</html> 

LoginController.java

package rajeev.spring.spitter.mvc.controller; 

import javax.validation.Valid; 

import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/login") 
public class LoginController { 

    @RequestMapping(value="/validatelogin", method=RequestMethod.POST) 
    public String validateLogin(@Valid @ModelAttribute("loginObj") LoginBean loginObj, BindingResult bindingResult) 
    { 
     System.out.println(bindingResult.hasErrors()); 
     if(bindingResult.hasErrors()) 
     { 
      return "hi"; 
     } 
     return "home"; 
    } 
} 

spitter-servlet.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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <mvc:annotation-driven/> 
    <context:component-scan base-package="rajeev.spring.spitter.mvc.controller"></context:component-scan> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
    <display-name>Spring Hello World</display-name> 
    <welcome-file-list> 
     <welcome-file>/</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>springDispatcher</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spitter-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>springDispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

在第二次提交 'hi.jsp爲' tomcat日誌也顯示警告:

WARNING: No mapping found for HTTP request with URI [/SpringMVC/login/login/validatelogin] in DispatcherServlet with name 'springDispatcher' 

看來,第二次提交的'hi.jsp'期間的額外'/login'正在追加到窗體的提交路徑。

請建議如果上述代碼有問題,或者需要修改它以使其正常工作。

感謝您的幫助。

回答

1

這是因爲相對錶單提交URL改變,當你映射你的處理方法爲不同的URL

<s:form method="POST" modelAttribute="loginObj" action="login/validatelogin"> 

這個問題常見的解決方案是使用絕對路徑,而不是硬編碼,查找使用的上下文路徑

<c:set var="root" value="${pageContext.request.contextPath}"/> 

然後在表單上

<s:form method="POST" modelAttribute="loginObj" action="${root}/login/validatelogin"> 

你可能會考慮其他選擇是使用後重定向模式在你的控制器處理方法,以避免切換URL

public String validateLogin(@Valid @ModelAttribute("loginObj") LoginBean loginObj, BindingResult bindingResult) { 
    .... 
    return "redirect:/login"; 
} 
+0

gerrytan:非常感謝...終於有效:) –

0

試試這個:

<c:url var="myAction" value="/login/validatelogin"/> 
<s:form method="POST" modelAttribute="loginObj" action="${myAction}"> 
+0

這不是preferrable因爲斜線可能擦掉上下文根。例如,如果應用程序使用context-root myapp:http:// myhost/myapp安裝,表單將發佈到http:// myhost/login/validatelogin而不是http:// myhost/myapp/login/validatelogin – gerrytan

相關問題