英文:
Moving objects from HTML page to jsp in SpringMVC
问题
以下是您要翻译的内容:
我正在进行一个项目,我需要在我的 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);
//
//
// 找出用户账户中有多少钱的某种方法。为了我们的目的,假设有 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()};
这行代码是错误的,但这是我想要实现的。请帮忙。
英文:
I am working on a project, and I need to access the data in the model class object in my jsp. My project is kind of big, so I'll post a problem that's similar and simpler.
Say there is a bank account with money in USD and I have to display in Indian rupees (1 USD = 70 rupees).
Account class:
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 class:
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>
Now, with the line:
int balance = ${account.getBalance()};
This line is wrong, but this is what I want to achieve. Please help.
答案1
得分: 2
更新后的 `user.jsp` 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="com.example.Account" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户主页</title>
</head>
<body>
<h3>你好,${account.userName}</h3>
<%
Account account = (Account) request.getAttribute("account");
int balance = account.getBalance() * 70;
out.println("余额为:" + balance + "卢比");
%>
</body>
</html>
英文:
The updated user.jsp
is :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="com.example.Account" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Home Page</title>
</head>
<body>
<h3>Hi ${account.userName}</h3>
<%
Account account = (Account) request.getAttribute("account");
int balance = account.getBalance() * 70;
out.println(" Balance is "+balance+" rupees");
%>
</body>
</html>
答案2
得分: 0
请注意的一点是,您可以直接使用属性名而不是 getters。
${account.userName}
并且可以使用启用表达式语言的 JSTL 来执行类似以下的乘法操作。
<c:set var="result" value="${balance * 70}"/>
英文:
One thing to note here is, you can use property name directly instead of getters.
${account.userName}
And use JSTL for expression language enabled can perform multiplication like below.
<c:set var="result" value="${balance * 70}"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论