我在我的项目中使用了spring 4 MVC,它是基于注释的。 我能够成功登录但我无法注销。
我已在下面发布了配置,初始化程序和控制器类 以及登录页面和欢迎页面。请告诉我需要对我的代码进行哪些更改才能成功注销。
我目前在退出时遇到以下错误
ViewModel
AppConfig_login
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'
}
AppInitializer_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;
}
AppController的
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[] { "/" };
}}
的login.jsp
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";
}}
welcome_student
<%@ 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>
答案 0 :(得分:0)
所以我终于发现问题出在我的jsp welcome_student
中我忘了包含以下内容
if 'next' in request.args:
return redirect(next)
else:
return redirect(url_for("user.index"))