将HTML页面中的对象移到SpringMVC的JSP页面中。

huangapple go评论99阅读模式
英文:

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 = &quot;/&quot;, method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		return &quot;home&quot;;
	}

	@RequestMapping(value = &quot;/user&quot;, method = RequestMethod.POST)
	public String user(@Validated String user, Model model) {
		System.out.println(&quot;User Page Requested&quot;);
		Account account = new Bank();
        account.setUserName(user);
        //
        //       
        //  Some way to figure how much money there is in the user&#39;s account. For our purposes, say $5.
        //
        //
        account.setBalance(5);
        model.addAttribute(&quot;account&quot;,account);
       	return &quot;user&quot;;
 	}
}

JSP:

user.jsp

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;User Home Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;Hi ${account.getUserName()}&lt;/h3&gt;
&lt;% 
    int balance = ${account.getBalance()};
    balance = balance*70;
    out.println(&quot; Balance is &quot;+balance+&quot; rupees&quot;);
%&gt;
&lt;/body&gt;
&lt;/html&gt;

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 :

   &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
    &lt;%@ page import=&quot;com.example.Account&quot; %&gt;  
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
    &lt;title&gt;User Home Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h3&gt;Hi ${account.userName}&lt;/h3&gt;
   &lt;% 
    Account account = (Account) request.getAttribute(&quot;account&quot;);
    int balance = account.getBalance() * 70;
    out.println(&quot; Balance is &quot;+balance+&quot; rupees&quot;);
   %&gt;
    &lt;/body&gt;
    &lt;/html&gt;

答案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.

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

huangapple
  • 本文由 发表于 2020年4月4日 23:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61030475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定