Я создаю приложение для входа в Spring mvc, поэтому при регистрации или входе в систему я просто получаю $ {attributetename} (в моем случае $ {firstname}) без фактического значения атрибута в welcome jsp. Пожалуйста, дайте мне знать, в чем проблема с моим кодом.
Userbean и welcome jsp имеют одно и то же имя.
LoginController
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping ("/login")
public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
ModelAndView mav = new ModelAndView("login");
mav.addObject("login",new Login());
return mav;
}
@PostMapping(value = "/loginProcess")
public ModelAndView loginProcess(Model model, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("login") Login login){
ModelAndView mav = null;
User user = userService.validateUser(login);
if(user!=null){
mav= new ModelAndView("welcome");
System.out.println("user.getFirstname(): "+user.getFirstname());
mav.addObject("firstname", user.getFirstname());
//model.addAttribute("firstname", "welcome"+user.getFirstname());
}
else
mav = new ModelAndView("login");
mav.addObject("error", "Username or password is wrong");
return mav;
}
}
логин jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ 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>login</title>
</head>
<body>
<form:form id="loginForm" modelAttribute="login" action="loginProcess" method="post">
<table align="center">
<tr>
<td>
<form:label path="username">Username: </form:label>
</td>
<td>
<form:input path="username" name="username" id="username" />
</td>
</tr>
<tr>
<td>
<form:label path="password">Password:</form:label>
</td>
<td>
<form:password path="password" name="password" id="password" />
</td>
</tr>
<tr>
<td></td>
<td align="left">
<form:button id="login" name="login">Login</form:button>
</td>
</tr>
<tr></tr>
<tr>
<td></td>
<td><a href="home.jsp">Home</a>
</td>
</tr>
</table>
</form:form>
<table align="center">
<tr>
<td style="font-style: italic; color: red;">${param.message}</td>
</tr>
</table>
</body>
</html>
welcome.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>Welcome</title>
</head>
<body>
<table>
<tr>
<td> ${firstname}</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td><a href="home.jsp">Home</a>
</td>
</tr>
</table>
</body>
</html>
UserBean:
public class User {
private String username;
private String password;
private String firstname;
private String lastname;
private String email;
private String address;
private int phone;
//getter, setter
}