项目添加Struts2容器,使用标签s:iterator来遍历输出。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta charset="utf-8">
</head>
<body>
This is my JSP page. <br>
<p><a href="display">display</a></p>
<!-- <s:bean name="top.k10000.Home" id="request"></s:bean> -->
<s:iterator value="#request.list">
<s:property value="id"/>
<s:property value="name"/>
<s:property value="pass"/>
<s:property value="sex"/>
<s:property value="age"/>
<s:property value="chara"/>
<br/>
</s:iterator>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="display" class="top.k10000.Home" method="display">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
Home.java
package top.k10000;
import java.util.ArrayList;
import org.hibernate.Session;
import com.opensymphony.xwork2.ActionSupport;
public class Home extends ActionSupport{
private static final long serialVersionUID = 1L;
private ArrayList<User> list;
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
public Home(){
Session session=HibernateSessionFactory.getSession();//通过HibernateSessionFactory类的getSession方法获得Session对象
ArrayList mylist=(ArrayList) session.createQuery("from User s").list();//执行HQL语句,查询内容,并转换为list集合
this.list = mylist;
for(int i=0;i<mylist.size();i++){
User stu=(User)mylist.get(i);//获取到对象的值
System.out.println("Id:"+stu.getId()+",Name:"+stu.getName()+",Sex:"+stu.getSex()+",Age:"+stu.getAge());//打印从stu对象里获取到的值
}
session.close();
}
public String display() {
return SUCCESS;
}
}
效果:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/151214.html