-1

I have used spring 4 MVC in my project which is annotation based. I am able to login successfully but I am not able to logout.

I have posted the configuration,initializer and controller classes below along with the login page and welcome page.Please let me know what changes need to be done to my code so that I log out successfully.

I am currently getting the below error on logout

Feb 06, 2017 2:36:03 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Student_Login/%3Cc:url%20value='/login'%20/%3E] in DispatcherServlet with name 'dispatcher'

AppConfig_login

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.student.login")
    public class AppConfig_login {

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
}

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("message");
    return messageSource;
}

}

AppInitializer_login

    public class AppInitializer_login extends      AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { AppConfig_login.class };
}

@Override

protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
     return null;}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}}

AppController

    package com.student.login.controller;

    import java.util.List;
    import java.util.Locale;



@Controller
@RequestMapping("/")
public class AppController_login {
    @Autowired
    StudentService service;

    @Autowired
    MessageSource messageSource;
    /*
     * This method will list all existing employees.
     */
    @RequestMapping(value = {"/"}, method = RequestMethod.GET)
    public String listStudents(ModelMap model) {
        //List<Student> students = service.findAllStudents();
        //model.addAttribute("students", students);
        Student student = new Student();
        model.addAttribute("student", student);
        return "login";
    }

    @RequestMapping(value = { "/" }, method = RequestMethod.POST)
    public String checkCredentials(@Valid Student student, BindingResult result,
            ModelMap model) {
        if (result.hasErrors()) {
            return "login";
        }
        System.out.println(student.getRoll_no());
        System.out.println(student.getPassword());

        /*
         * Preferred way to achieve uniqueness of field [ssn] should be implementing custom @Unique annotation 
         * and applying it on field [ssn] of Model class [Employee].
         * 
         * Below mentioned peace of code [if block] is to demonstrate that you can fill custom errors outside the validation
         * framework as well while still using internationalized messages.
         * 
         */
        if(service.isCredentialValid(student.getRoll_no(),student.getPassword())){
            FieldError login_Error =new FieldError("student","password",messageSource.getMessage("non.unique.credentials", new String[]{student.getRoll_no()}, Locale.getDefault()));
            result.addError(login_Error);
            return "login";
        }


        //service.saveStudent(student);
        model.addAttribute("welcome", "Welcome Student " + student.getName() + " registered successfully");
        return "welcome_student";
    }

    @RequestMapping(value = { "/login" }, method = RequestMethod.GET)
    public String logoutPage(ModelMap model) {
        Student student = new Student();
        model.addAttribute("student", student);
        //model.addAttribute("edit", false);
        return "login";
    }




    /*
     * This method will provide the medium to add a new employee.
     */
    @RequestMapping(value = { "/registration" }, method = RequestMethod.GET)
    public String newStudent(ModelMap model) {
        Student student = new Student();
        model.addAttribute("student", student);
        model.addAttribute("edit", false);
        return "registration";
    }
    /*
     * This method will be called on form submission, handling POST request for
     * saving employee in database. It also validates the user input
     */
    @RequestMapping(value = { "/registration" }, method = RequestMethod.POST)
    public String saveEmployee(@Valid Student student, BindingResult result,
            ModelMap model) {
        if (result.hasErrors()) {
            return "registration";
        }

        if(!service.isStudentRollNoUnique(student.getRoll_no())){
            FieldError roll_no_Error =new FieldError("student","roll_no",messageSource.getMessage("non.unique.roll_no", new String[]{student.getRoll_no()}, Locale.getDefault()));
            result.addError(roll_no_Error);
            return "registration";
        }
        service.saveStudent(student);
        model.addAttribute("success", "Student " + student.getName() + " registered successfully");
        return "login_success";

        }}

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Student Registration Form</title>

<style>

    .error {
        color: #ff0000;
    }
</style>

</head>

<body>

    <h2>Login Form</h2>

    <form:form method="POST" modelAttribute="student">
    <!--    <form:input type="hidden" path="id" id="id"/>-->
        <table>
            <tr>
                <td><label for="roll_no">Roll No:</label> </td>
                <td><form:input path="roll_no" id="roll_no"/></td>
                <td><form:errors path="roll_no" cssClass="error"/></td>
            </tr>
            <tr>
                <td><label for="password">Password:</label> </td>
                <td><form:input path="password" id="password"/></td>
                <td><form:errors path="password" cssClass="error"/></td>
            </tr>
            <tr>
                <td colspan="3">

        <input type="submit" value="Submit"/>
                </td>
            </tr>
        </table>
    </form:form>
    <br/>
    <br/>
    Go back to <a href="<c:url value='/registration' />">Registration</a>
</body>
</html>

welcome_student

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello user



     <a href="<c:url value='/login' />">Logout</a>


</body>
</html>
nats
  • 1
  • 3
  • 1
    Why does your logout button redirect to `/login` and why is your logout method mapping `/login`? – qwerty1423 Feb 06 '17 at 23:10
  • @qwerty1423 I want to redirect it to the login page when I click on logout link.Can you please tell me what should be the mapping?This is where I need help. – nats Feb 07 '17 at 00:29
  • You can redirect to login page after the mapping (on return), but you need unique mapping (eg. /logout) to enter the method. – qwerty1423 Feb 07 '17 at 08:48
  • @qwerty1423 Can you please elaborate and if possible show me exactly where the change needs to be done. – nats Feb 07 '17 at 20:33

1 Answers1

0

So I finally figured out that the problem was in my jsp welcome_student

I forgot to include the below line

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
nats
  • 1
  • 3