2014-03-27 118 views
-1

我是Spring MVC的新手。 請原諒,如果這是一個愚蠢的問題,我已經嘗試在自己身上,但也沒能解決控制器在Spring MVC中沒有調用

我面對這裏的問題是我的控制器是永遠不會被獲取調用

這裏是代碼

的index.jsp

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form action="/hello.html" method="post"> 
Name:<input type="text" name="name"/><br/> 
Password:<input type="password" name="password"/><br/> 
<input type="submit" value="login"/> 
</form> 

</body> 
</html> 

調度-servlet.xml中

<context:component-scan base-package="com.javatpoint" /> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

hellopage.jsp

Message is: ${message} 

的errorPage.jsp

${message} 
<jsp:include page="/index.jsp"></jsp:include> 

HelloWorldController.java

@Controller 
public class HelloWorldController { 

    @RequestMapping("/hello") 
    public ModelAndView helloWorld(HttpServletRequest request,HttpServletResponse res) { 
     String name=request.getParameter("name"); 
     String password=request.getParameter("password"); 

     System.out.println("The Name is"+name); 
     System.out.println("The password is"+password); 

     if(password.equals("admin")){ 
     String message = "HELLO "+name; 
     return new ModelAndView("hellopage", "message", message); 
     } 
     else{ 
      return new ModelAndView("errorpage", "message","Sorry, username or password error"); 
     } 
    } 

} 

的web.xml

<servlet> 
<servlet-name>dispatcher</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>dispatcher</servlet-name> 
<url-pattern>*.html</url-pattern> 
</servlet-mapping> 
</web-app> 

回答

1

在表單中應用操作上下文路徑:

<form action="${pageContext.request.contextPath}/hello.html" method="post"> 
+0

我已經更新了我的問題(web.xml),它與您提到的相同,但仍然不起作用。 – Pawan

+0

找出發生了什麼的最好方法是在log4j.xml配置中調試此appender:org.springframework.web。更新你的問題與日誌輸出... – codependent

+0

忘記它,我想我知道什麼是錯的,檢查新的答案 – codependent

0

試着改變你的窗體的行動從/hello.html/hello,因爲這是你在你的控制器的@RequestMapping

+0

謝謝,我已經這樣做了,但它不工作。 – Pawan

+0

可能還有其他錯誤,但您也必須進行此更改才能正常工作。還要確保已經重新編譯和重新部署了所有內容,並清除了瀏覽器的緩存。 – GreyBeardedGeek

相關問題