开发者社区 问答 正文

需要一种使显示方法充满数据以在jsp上显示的方法

我正在处理银行申请。尽管我似乎无法弄清楚如何使显示方法打印到客户端而不是服务器日志,但我的大多数业务对象都能正常工作。对大多数人来说,这似乎很容易,但我还是有些困惑。

这是jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import= "business.AccountList" %>
<%@page import= "business.Customer" %><!--Space between =" makes the String orange-->
<%@page import= "business.Account" %><!--Space between =" makes the String orange
                                       or black.--> 
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

    <html lang="en-US">
<head>
      <meta charset="UTF-8" />
      <meta name="description" content="Display Accounts Page" />
      <meta name="keywords" content="HTML, Java" />
      <meta name="author" content="Robert McGuire " />
      <!--<meta http-equiv="refresh" content="30" />-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Display Accounts Page</title>
      <!--<link rel="stylesheet" type="text/css" href="style_General.css" />-->
      <!--[if lt IE 9]>
        <script src="/js/html5shiv.js"></script>
      <![endif]-->
</head>
    <body>
        <%
            Customer c1 = (Customer)session.getAttribute("c1");
            out.println("Customer object from session:"+c1);
            //Account a1 = (Account)session.getAttribute("a1");
            //out.println("Account object from session:"+a1);
            //c1.display();
            //c1.list.arr[0].display();
            int c =c1.list.count;
            for(int i =0; i<c;i++){
                c1.list.arr[i].display();
            }
            %>

            <h1>Welcome <%=c1.getCustFirstName()%>    <%=c1.getCustLastName()%> .</h1>
            <p>Address : <%=c1.getAddress()%> </p>
            <p>Email : <%=c1.getEmail()%> </p>

            <h2>Your Account details are listed here.</h2>
        <!--Form Section.-->
        <div>

          <!--  <form action="http://localhost:8080/WebApplication12/AccountLookupServlet" method="post">
                <pre>
                Account No: <input type="text" name="accountNumber" placeholder="Account ##"><br>
                Customer ID:<input type="text" name="custId" placeholder="Customer ID"/><br>
                Type:       <input type="text" name="type" placeholder="Account Type"/><br/>
                Balance:    <input type="text" name="balance" placeholder="Balance"/>
                <br/>        
                            <input type="submit" value="Retrieve" /><input type="reset" value="Clear" />
                </pre>
            </form>-->
        </div>
        <!--putting the acct info back into the page not into the textboxes. -->
        <div>
            <table>
                <thead>Account Information</thead>
                <hr/>
                <br/>  
                <tr>Account Number:<%= c1.list.arr[c]%></tr><br/>
           <tr> _________Display___________</tr><br/>
           <tr> Customer Id:<%c1.getCustId(); %></tr><br/>
<%--            <tr> Account Type:<%c1.getAccounts() %> --%>

<!-- Info:   Account Type: Checking -->
<!-- Info:   Account Balance: 1000.0 -->
            </table>


        </div>
             <%
             //Putting the objects back into the session . 
             session = request.getSession();
             //session.setAttribute("a1", a1);
             session.setAttribute("c1", c1);



             %>



        <hr />
      <!--Footer section with all the navigation links.-->
        <footer>
            <a href="index.jsp" style="text-decoration: none">Homepage</a>
            <a href="login.jsp" style="text-decoration: none">Login Page</a> 
        </footer>
    </body>
</html>

控制台显示有效...我只是想不通如何将数据从Account对象获取到jsp。

香港专业教育学院试图添加JspWriter作为由display()接受的参数。我无处可去 我一直在阅读有关此JspWriter的文章,但不了解如何正确使用它。我要做的就是访问:

public void display() {
        System.out.println("_________Display___________");
        System.out.println("                           ");
        System.out.println("Account Number: " + getAccountNo());
        System.out.println("Customer Id:" + getCustomerID());
        System.out.println("Account Type: " + getAccountType());
        System.out.println("Account Balance: " + getBalance());

这从提供的jsp页面开始。我不知道如何使用JspWriter来完成此任务。

真诚的感谢。

ps。在不了解其他技术之前,我将使用scriptlet标签。我从未得知使用scriptlet标签是不好的做法。

展开
收起
垚tutu 2019-12-12 10:01:09 673 分享 版权
阿里云 AI 助理回答

在JSP中,直接使用脚本元素(scriptlet)确实可以完成任务,但现代Web开发实践更倾向于使用JSTL(Java Server Pages Standard Tag Library)和EL(Expression Language)来分离表示逻辑与业务逻辑,减少代码的复杂性和提高可维护性。不过,针对您的问题,我会先展示如何用您当前的方式解决问题,然后再简要介绍如何过渡到更佳实践。

使用Scriptlet解决当前问题

要在JSP页面中显示Account对象的信息,您需要将该对象传递给JSP页面,并在页面内使用脚本元素访问它的属性。假设您已经在Servlet或某个控制器类中创建了Account对象并将其放入请求作用域:

// 在Servlet或控制器中
Account account = ...; // 初始化Account对象
request.setAttribute("account", account);
request.getRequestDispatcher("yourJspPage.jsp").forward(request, response);

然后,在JSP页面中,您可以这样使用脚本元素来访问Account对象的属性:

<%-- 注意:以下为示例,实际开发推荐使用JSTL和EL --%>
<%
    Account account = (Account) request.getAttribute("account");
    if (account != null) {
%>
        <tr>
            <td>Account Number:</td>
            <td><%= account.getAccountNo() %></td>
        </tr>
        <tr>
            <td>Customer Id:</td>
            <td><%= account.getCustomerID() %></td>
        </tr>
        <tr>
            <td>Account Type:</td>
            <td><%= account.getAccountType() %></td>
        </tr>
        <tr>
            <td>Balance:</td>
            <td><%= account.getBalance() %></td>
        </tr>
<%
    }
%>

过渡到更好的实践

长远来看,建议采用JSTL和EL来替换脚本元素,以提升代码的清晰度和可维护性。首先,确保您的项目依赖中包含了JSTL库。在Maven项目中,可以在pom.xml添加如下依赖:

<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

然后,在JSP页面中使用JSTL和EL来显示数据,无需任何脚本元素:

<table>
    <tr>
        <td>Account Number:</td>
        <td>${account.accountNo}</td>
    </tr>
    <tr>
        <td>Customer Id:</td>
        <td>${account.customerID}</td>
    </tr>
    <tr>
        <td>Account Type:</td>
        <td>${account.accountType}</td>
    </tr>
    <tr>
        <td>Balance:</td>
        <td>${account.balance}</td>
    </tr>
</table>

通过这种方式,您的视图层会更加简洁、易于阅读和维护,同时遵循了MVC设计模式的最佳实践。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址: