gpt4 book ai didi

java - 如何使用 ModelAndview 将模型(bean)值从 Controller 传递到 jsp 页面?

转载 作者:行者123 更新时间:2023-12-02 10:23:54 26 4
gpt4 key购买 nike

我正在创建 Spring mvc 登录应用程序,因此在注册或登录时,我只是获得 ${attributename} (在我的例子中为 ${firstname}),而在welcome jsp 中没有实际的属性值。请让我知道我的代码有什么问题。Userbean 和welcome jsp 具有相同的属性名称。

登录 Controller :

    @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>

用户bean:

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
}

最佳答案

你能在你的jsp页面中修改这个吗?

    <tr>
<td> "${firstname}"</td>
</tr>

刚刚添加了双引号。还添加

<%@ page isELIgnored="false" %>

将其添加到您的 JSP 文件中。

关于java - 如何使用 ModelAndview 将模型(bean)值从 Controller 传递到 jsp 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54123782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com