-1
私は、アノテーションベースのプロジェクトでspring 4 MVCを使用しました。 ログインに成功しましたが、ログアウトできません。スプリング4を使用してログアウトできないMVC
の下に設定、初期化子、コントローラのクラスを投稿しました。ログインページとウェルカムページに加えて、私は正常にログアウトするためにコードにどのような変更が必要かを教えてください。
私は現在、ログアウト時に以下のエラーを取得しています
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>
あなたのログアウトボタンは '/ login'にリダイレクトされ、なぜあなたのログアウト方法は'/login'にマッピングされていますか? – qwerty1423
@ qwerty1423ログアウトリンクをクリックすると、ログインページにリダイレクトしたいのですが、マッピングをどうすればいいのか教えてください。 – nats
マッピングの後にログインページにリダイレクトすることができますが、メソッドに入るために一意のマッピング(たとえば、/ logout)が必要です。 – qwerty1423