简体   繁体   English

在 SpringMVC 中将对象从 HTML 页面移动到 jsp

[英]Moving objects from HTML page to jsp in SpringMVC

I am working on a project, and I need to access the data in the model class object in my jsp.我正在做一个项目,我需要访问我的 ZEC407CCE6B649DAA8E3EZ776 中的 model class object 中的数据。 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).假设有一个以美元计价的银行账户,我必须以印度卢比(1 美元 = 70 卢比)显示。

Account class:帐户 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:小服务程序 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: JSP:

user.jsp用户.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.请帮忙。

The updated user.jsp is:更新后的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>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>

One thing to note here is, you can use property name directly instead of getters.这里要注意的一件事是,您可以直接使用属性名称而不是 getter。

${account.userName}

And use JSTL for expression language enabled can perform multiplication like below.并且使用 JSTL 启用表达式语言可以执行如下乘法运算。

<c:set var="result" value="${balance * 70}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM