【问题标题】:how to display data dynamically on html page via jsp session如何通过jsp会话在html页面上动态显示数据
【发布时间】:2016-06-16 18:11:43
【问题描述】:

我有一个项目符号格式的文本区域,我想通过会话以项目符号格式在 html 中显示该数据。

那么如何发送该数据?每个用户数据包含并且长度不同,例如(1 个用户使用 3 个项目符号点和 2 个用户使用 5 个项目符号点)如何在 html 页面和显示上发送动态数据。未使用数据库..

【问题讨论】:

  • 您尝试过的信息和代码不多。也许<c:forEach 是你的朋友。

标签: java jsp session jakarta-ee


【解决方案1】:

识别和解析每个文本区域的要点(比如说帖子)

class BulletPoint{
  String content;
  public BulletPoint(String content){
        super();
        this.content=content;
     }
   //put public getters and setters here
}

class Post{
 List<BulletPoint> bulletPoints=new ArrayList<>();
 //put public getters and setters here
}

@WebServlet("/posts")
class DisplayPostServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse  response)
        throws IOException {
    //parse and prepare text area contents to list
    //for each text area 
       //new Post p
        //create bulletin list for post
        //set bulletins to post
    //end for 

    //simple example 
    List<Post> posts=new ArrayList<>();
    for(int i=1;i<5;i++){
      Post newPost=new Post();
        for(int j=1;j<=i;j++){
          Bulletin bulletin=new Bulletin("bulletin content"+j);
          newPost.getBulletins().add(bulletin);
        }

       posts.add(newPost);
    }


   //add posts in request scope, and forward to jsp page
   request.setAttribute("posts",posts);
   request.getRequestDispatcher("/displayPosts.jsp").forward(request,                response);
  }


}

displayPosts.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
 <head>

     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>


  <div>
     <c:forEach var="singlePost"  items="${posts}" varStatus="loopIndex">
      <ul> Post ${loopIndex}
        <c:forEach var="singleBulletin"  items="${singlePost.bulletins}">
            <li><c:out value="${singleBulletin.content}"/>
            </li>
         </c:forEach>
      </ul>
    </c:forEach>
 </div>



</body>

</html>

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2017-12-25
    • 2019-04-02
    • 2016-09-15
    相关资源
    最近更新 更多