-1

I am new to Spring MVC . please excuse if this is a dumb question , i have tried a lot on my own , but couldn't able to resolve

I am facing here a issue which is my controller is never being getting called

Here is the code

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>

dispatcher-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>

2 Answers 2

2

Include your app context path in the form action:

<form action="${pageContext.request.contextPath}/hello.html" method="post">  
6
  • I have updated my question (web.xml) it is same as you mentioned , but still its not working .
    – Pawan
    Mar 27, 2014 at 13:34
  • The best way to find out what's going on is configuring this appender at debug in your log4j.xml config: org.springframework.web. Update your question with the log output... Mar 27, 2014 at 13:43
  • Forget it, I think I know what's wrong, check the new answer Mar 27, 2014 at 13:46
  • excellent , now its working , thank you very very much . But why it has been mantadaory to use pageContext.request.contextPath in my case , which was not mentioned in the tutorial i was following .
    – Pawan
    Mar 27, 2014 at 13:50
  • if you use / at the beginning of the action attribute, it appends what you write to the server info in the url, so you lose the contextPath info. It's a good practice to use what I posted. Anyway, try action="hello.html" it may work. Mar 27, 2014 at 13:56
0

Try changing your form's action from /hello.html to /hello, as that's what you have in your controller's @RequestMapping

1
  • There may also be so ething else wrong, but you must also make this change for it to work. Also make absolutely sure that you've recompiled and redployed everything, and that you clear your browser's cache. Mar 27, 2014 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.