我正在创建Spring mvc登录应用程序,因此在注册或登录时,我只是获得$ {attributename}(在我的情况下为$ {firstname}),而没有在Welcome JSP中获得实际属性值。请让我知道我的代码有什么问题。 Userbean和Welcome JSP具有相同的服装名称。
LoginController :
(something falsy) && (anything) will always return false.
登录jsp:
@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;
}
}
welcome.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>
userbean:
<%@ 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>
答案 0 :(得分:1)
您能在jsp页面中对此进行修改吗?
<tr>
<td> "${firstname}"</td>
</tr>
只需添加双引号即可。同时添加
<%@ page isELIgnored="false" %>
这到您的JSP文件。