Struts2-action到jsp传值(传递查询到的所有数据)

思路:

1、action获取到数据,封装成list

2、通过ACtionContext或者Request将list放入其中

3、在jsp页面读取


底层的代码这里不讲述了,用的是mybatis与spring


Action代码:

package com.bs.view.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.bs.container.ServiceProvider;
import com.bs.po.DataList;
import com.bs.service.DataService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/*
 *@Author swxctx
 *@time 2017年5月1日
 *@Explain:获取所有数据信息
 */
public class DataAllAction extends ActionSupport{
private static final long serialVersionUID = 1L;
	
	/*加载applicationContext.xml*/
	private DataService dataService = (DataService)ServiceProvider.getService(DataService.SERVICE_NAME);
	
	private List<DataList> dataLists;

	public List<DataList> getDataLists() {
		return dataLists;
	}
	public void setDataLists(List<DataList> dataLists) {
		this.dataLists = dataLists;
	}

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*获取所有数据信息*/
		dataLists = dataService.findAllData();//(调用service层的代码)
		//request.setAttribute("dataLists", dataLists);
		ActionContext.getContext().put("dataLists", dataLists);
		return "success";
	}
}

Jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>

<table width="100%" border=1>
	<tr>
		<td>num</td>
		<td>temp</td>
		<td>co</td>
		<td>ip</td>
		<td>time</td>
	</tr>
	
	<s:iterator value="dataLists">
		<tr>
			
			<td><s:property value="num"/></td>
			<td><s:property value="temp"/></td>
			<td><s:property value="co"/></td>
			<td><s:property value="ip"/></td>
			<td><s:property value="time"/></td>
			
		</tr>
	</s:iterator>

</table>

</body>
</html>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章