【发布时间】:2020-04-04 15:33:12
【问题描述】:
我正在做一个项目,我需要在我的jsp中访问模型类对象中的数据。我的项目有点大,所以我会发布一个类似且更简单的问题。
假设有一个以美元计价的银行账户,我必须以印度卢比(1 美元 = 70 卢比)显示。
账户类:
public class Account
{
private String userName;
private int balance;
public String getUserName() {
return userName;
}
public int getBalance(){return balance;}
public void setUserName(String userName) {
this.userName = userName;
}
public void setBalance(int balance) {
this.balance = balance;
}}
Servlet 类:
public class HomeController {
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String user(@Validated String user, Model model) {
System.out.println("User Page Requested");
Account account = new Bank();
account.setUserName(user);
//
//
// Some way to figure how much money there is in the user's account. For our purposes, say $5.
//
//
account.setBalance(5);
model.addAttribute("account",account);
return "user";
}
}
JSP:
user.jsp
<!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=UTF-8">
<title>User Home Page</title>
</head>
<body>
<h3>Hi ${account.getUserName()}</h3>
<%
int balance = ${account.getBalance()};
balance = balance*70;
out.println(" Balance is "+balance+" rupees");
%>
</body>
</html>
现在,换行:
int balance = ${account.getBalance()};
这条线是错误的,但这是我想要实现的。请帮忙。
【问题讨论】:
标签: java spring spring-mvc jsp