Я работаю над проектом, и мне нужно получить доступ к данным в объекте класса модели в моем 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;
}}
Класс сервлета:
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()};
Эта строка неверна, но это то, чего я хочу достичь. Пожалуйста, помогите.