简体   繁体   English

需要使用struts 1.3在jsp页面上显示数据

[英]Need to display data on a jsp page using struts 1.3

I am trying to read data from a table and display it to the user . 我正在尝试从表中读取数据并将其显示给用户。

Can anybody tell how to do it using struts 1.3 ? 任何人都可以告诉如何使用struts 1.3做到这一点?

  1. Write an class extending Struts' Action class. 写一个扩展Struts的一类Action类。 This class pulls the data from Database as a List . 此类将数据作为List从数据库中提取。 Pass this data as request attribute, request.setAttribute("myList", list) . 将此数据作为请求属性传递, request.setAttribute("myList", list) Return "success". 回归“成功”。

  2. In your struts-config.xml , map this Action class to a JSP on "success". struts-config.xml ,将此Action类映射到“success”上的JSP。 The request will be forwarded to the JSP. 请求将被转发到JSP。

  3. In the JSP, get the list from request by request.getAttribute("myList") . 在JSP中,通过request.getAttribute("myList")从请求中获取列表。 Iterate through the list and print the List . 遍历列表并打印List

You need to study this: http://struts.apache.org/1.x/userGuide/index.html 你需要研究这个: http//struts.apache.org/1.x/userGuide/index.html

(Edit: Just noticed this is a 2 year old question) (编辑:刚刚注意到这是一个2岁的问题)

Don't use the struts tags unless you NEED to. 除非您需要,否则请勿使用struts标签。 This can be accomplished with jstl/el. 这可以通过jstl / el完成。 So on your Action class you would have something like this: 所以在你的Action类中你会有这样的东西:

List<Map<?, ?>> listOfHashMaps = new ArrayList<Map<?, ?>>();
request.setAttribute("listOfHashMaps", listOfHashMaps);

In your jsp: 在你的jsp中:

<c:forEach var="hashMap" items="listOfHashMaps">
    ${hashMap[someInteger]} <%-- To get the value associated with 'key' --%>
</c:forEach>

You can also access the keys/values with: 您还可以使用以下方法访问键/值:

${hashMap.key}
${hashMap.value}

Respectively. 分别。

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

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