У меня есть простое приложение для входа в Spring mvc SpringHibernateLogin с проверкой формы. Проверка работает нормально, что происходит в случае пропуска имени пользователя / пароля или даже для неверных учетных данных, и возвращает меня на ту же страницу входа с соответствующим сообщением об ошибке. , но отображаемый URL неверен. Во всех случаях к URL добавляется страница приветствия (например, http://localhost:8080/SpringHibernateLogin/login1/hello1.jsp),here, в моем случае страница приветствия hello1.jsp. В идеале она должна возвращать только http://localhost:8080/SpringHibernateLogin/login1.
home.jsp
<%@ 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>Home</title>
</head>
<body>
<h2 align="center" style="color:Tomato">Spring Hibernate Demo</h2>
<a href="login1">Existing user</a>
<br><br>
</body>
</html>
login1.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"%>
<!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>Login</title>
</head>
<body>
<form:form name="frm" method="post" action="hello1" modelAttribute="log1">
<form:label path="username">USERNAME</form:label>
<form:input path="username"/>
<form:errors path="username" cssClass="error c"></form:errors>
<br><br>
<form:label path="password">PASSWORD</form:label>
<form:input path="password" type="password"/>
<form:errors path="password" cssClass="error"></form:errors>
<br><br>
<input type="submit" value="SUBMIT"/>
</form:form>
</body>
</html
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringHibernateLogin</display-name>
<welcome-file-list>
<welcome-file>login1.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-
mapping>
</web-app>
spring1-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.jt.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/classes/messages"/>
</bean>
</beans>
HelloController.java
package com.jt.shlogin;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.jt.model.login1model;
import com.jt.validator.LoginClientSideValidation;
@Controller
public class HelloController{
@RequestMapping("/home")
public String homepage(Model model)
{
return "home";
}
@RequestMapping("/login1")
public String firstPage(Model model) {
model.addAttribute("log1",new login1model());
return "login1";
}
@RequestMapping("/hello1")
public ModelAndView printHello(@ModelAttribute("log1")login1model log1,ModelMap model,BindingResult result, HttpServletRequest req) {
ModelAndView model1=null;
LoginClientSideValidation v=new LoginClientSideValidation();
v.validate(log1,result);
if(result.hasErrors())
{
model1= new ModelAndView("login1");
}
else
{
String name = req.getParameter("username");
model.addAttribute("message1", "Hi "+name+" welcome for Spring MVC Training");
model1=new ModelAndView("hello1");
}
return model1;
}
}
LoginClientSideValidation.java
package com.jt.validator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.jt.model.login1model;
@Component
public class LoginClientSideValidation implements Validator {
@Override
public boolean supports(Class<?> arg0) {
return login1model.class.isAssignableFrom(arg0);
}
public void validate(Object arg0, Errors arg1) {
login1model log1=(login1model)arg0;
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "username", "log1.username.empty");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "password", "log1.password.empty");
}
}
messages.properties
log1.username.empty="Username cannot be empty";
log1.password.empty="Password cannot be empty";
Структура папок
структура папок