【问题标题】:Moving objects from HTML page to jsp in SpringMVC在 Spring MVC 中将对象从 HTML 页面移动到 jsp
【发布时间】: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


    【解决方案1】:

    更新后的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>
    

    【讨论】:

    • 现在我试了一下,没有“request.getAttribute("account")”。该函数在响应中不存在
    • 但是先生,每当我使用该对象时,页面都会变为空白并且不显示任何内容,但对于 css 部分。
    • @Shanky301 我已经更新了我的答案。它现在应该可以工作了。
    • 也许在这里。但在实际场景中,我非常需要这个先生。
    • 谢谢先生。现在明白了
    【解决方案2】:

    这里要注意的一点是,您可以直接使用属性名称而不是 getter。

    ${account.userName}
    

    并使用 JSTL 启用表达式语言可以执行如下乘法。

    &lt;c:set var="result" value="${balance * 70}"/&gt;

    【讨论】:

    • 但我需要它作为jsp中的对象
    • 帐户只是一个对象,这就是我们从中检索用户名的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多