0

Can you please help me figure out how to invoke the login page by using my controller?

Here is my code:

package com.mvc.demo;

public class Emp {
     private String name;
     private String password;

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public String getPassword() {
         return password;
     }

     public void setPassword(String password) {
         this.password = password;
     }

} 

MvcDemo.java (it's my controller; just for invoking login page)

package com.mvc.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class MvcDemo {

     @RequestMapping(value="/login", method = RequestMethod.GET)
     public String showForm(Emp em) {
          return "login";
     } 
}

dispatcher-servlet.xml

<context:component-scan base-package="com.mvc.demo" />
<mvc:annotation-driven />
<beans>
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

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>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

login.jsp

 <form:form action="#" method = "post" modelAttribute="emp">
        <form:label path="username">Enter your user-name</form:label>
        <form:input id="username" name="username" path="name" /><br>
        <form:label path="username">Please enter your password</form:label>

        <form:password id="password" name="password" path="password" /><br>

        <input type="submit" value="Submit" />
 </form:form>

project Structure:

   MvcDemo
     JavaResources
      src
       com.mvc.demo
    WebContent
      jsp
        login.jsp
    WEB-INF
     lib
     web.xml
     dispatcher-servlet.xml
   index.jsp  
4

3 Answers 3

1

You are missing @Controller annotation on your controller class. Spring does not create a handler for url unless you instantiate a controller by using annotation.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you satya still getting sam error http 404 MvcDemo/login not found
Ya like Initialising Spring framework servlet 'dispatcher'
Configure your prefix as /WEB-INF/jsp/ and move your jsp folder inside WEB-INF folder
yes I have done this also. Actually I ve done another example that's working fine. but this example isn't working. anyway thanks a lot satya
0

It does the same as in your code,when you hit url with localhost:8080/Mvc_Demo/login it must show your login.jsp, hope this solves your problem.

package com.mvc.demo;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    import org.springframework.web.servlet.ModelAndView;

    public class MvcDemo {
    @RequestMapping(value="/login", method = RequestMethod.GET)

     public String showForm( )
     {
       ModelAndView mv = new ModelAndView("login");
       return mv;


       } 
     }

1 Comment

thanks a lot Punjan, but its not working even u r returned modelandview but method expecting String type. I have changed and executed even it seems same problem http status 404 error
0

Try this code:

package com.mvc.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MvcDemo {

    // To call the view for login
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login() {
         return new ModelAndView("login","newEmp", new Emp());
    }

    // To call the validate login after submit
    @RequestMapping(value = "/user-login", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView userLogin(Emp emp) {
         //TODO check 'emp' object to validate user
         return new ModelAndView("home");
    }


}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.