struts2中从数据库中读取数据,并在JSP页面中遍历保存有JavaBean对象的List对象

时间:2022-06-05 19:41:07

0x1:前言

前面搭建struts就不说了,大家都能成功完成。


0x2:Model

这里我们需要一个Model类来接收

<span style="font-size:10px;font-weight: normal;">package com.model;

public class BlogContent {


	private int id;
	private String title;
	private String content;
	
	public BlogContent(int id, String title, String content) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	
	
}</span><span style="color:#ff6666;font-size:18px;">
</span>


d dsfadd dsfasdf

0x3:Action处理control

首先创建一个Action类,用来处理我们的请求。由于是测试,写的代码比较乱,大家将就看下。

package com.wzw;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.model.BlogContent;
import com.opensymphony.xwork2.ActionSupport;
import com.wzw.db.dbUtil;

public class BlogContentAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7223376836405887303L;

	
	@Override
	public String execute() throws Exception {
		// TODO 自动生成的方法存根
		List<BlogContent> BlogContentlist=new ArrayList<BlogContent>();
		String sql="select * from content";
		dbUtil dbutil=new dbUtil();
		Connection con=dbutil.getCon();
		PreparedStatement pstmt= con.prepareStatement(sql);
		ResultSet rs=pstmt.executeQuery();
		while(rs.next())
		{
			int id=rs.getInt("id");
			String title=rs.getString("title");
			String content=rs.getString("content");
			BlogContent temp=new BlogContent(id,title,content);
			BlogContentlist.add(temp);
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("bloglist", BlogContentlist);
		return SUCCESS;
	}
	
	
	

}

PS:由于我之前用的这张表,字段分别是id主键,title 和content,用来测试。


0x4 映射

配置struts.xml文件。增加我们的映射。

        <action name="BlogContent" class="com.wzw.BlogContentAction" >
            <result name="success">/1.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>

访问以后跳转到1.jsp上去。

0x5 显示层 View

编写1.jsp来遍历我们request中存放的数据,代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>Insert title here</title>
</head>
<body>
	<s:iterator value="#request.bloglist" id="mycontent" status="status">
		<s:property value="#mycontent.id"/><br>
		<s:property value="#mycontent.title"/><br>
		<s:property value="#mycontent.content"/><br><hr>
	</s:iterator>
</body>
</html>

0x6 result

访问我们的映射地址

http://localhost:8080/struts//BlogContent


结果如下

1
这是测试标题1
这是测试标题内容1


2
Nothing is as sweet as you
Nothing is as sweet as you.....

0x6 END

 END