源码地址:https://download.csdn.net/download/u013636987/11387400
上一篇:系列一:struts2框架搭建
Struts和JSP直接的参数传递。
提交数据:from方式提交
jsp端:
<form id="Form2UpLoad" name="post" action="logger_downfiles.action" method="post">
<input type="hidden" id="files_path" name="files_path" />
<input type="hidden" id="files_name" name="files_name" />
</form>
Action端:
1、在acton中直接创建变量
private String files_path;
然后创建他的set和get方法,一定不能忘记。
在代码中正常对files_path进行读写。这个变量和jsp中的name=files_path是一一对应的,值也是一样的。
2、使用ModelDriven接口
实现getModel这个方法,该方法返回的是你的ModelDrivern泛型中设定的变量的实体类,如下
public class LoggerAction extends ActionSupport
implements ModelDriven<Logger>
{
// 不要忘记手动new
private Logger logger = new Logger();
public Logger getModel() {
return logger;
}
public InputStream getDownloadFile() {
try {
InputStream in = new FileInputStream(new File(logger.getFiles_path()));
return in;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ServletActionContext.getServletContext().getResourceAsStream(logger.getFiles_path());
}
}
在logger类中进行定义和get,set。
public class Logger {
private String files_path;
public String getFiles_path() {
return files_path;
}
public void setFiles_path(String files_path) {
this.files_path = files_path;
}
}
返回数据 使用Session
action端:返回列表数据
public String showlist() {
System.out.println("执行showlist");
System.out.println("logger=" + logger.toString());
List<Logger> loggerlist = loggerService.findAll();
int i = 0;
List<JSONObject> listJson = new ArrayList<JSONObject>();
;
for (Logger newlogger : loggerlist) {
i++;
System.out.println("logger=" + newlogger.toString());
JSONObject json = null;
json = (JSONObject) JSONObject.toJSON(newlogger);
System.out.println("json" + json.toString());
listJson.add(json);
}
ServletActionContext.getRequest().getSession().setAttribute("loggerList", listJson);
return SUCCESS;
}
js端:接收列表数据
var json1 = '${sessionScope.loggerList}';
var obj = JSON.parse(json1);
提交数据 使用
action端 返回json数据
public String showData()
{
System.out.println("执行showData");
//查询用户信息
List<Authorization> list = authorizationService.findAll();
//封装好的用户信息放到json中返回前台
String json = null;
json = JSON.toJSONString(list);
System.out.println("list"+list.get(0).getAddress());
System.out.println("json"+json.toString());
ServletUtils.flushData(response,json.toString());
return NONE;
}
public static void flushData(HttpServletResponse response,String json) {
byte[] jsonBytes;
try {
jsonBytes = json.getBytes("utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
js中接收,(DataGrid控件后续系列再做介绍)
$('#excel').datagrid({
type : 'post',
url : 'authorization_showData.action',
loadMsg : '数据加载中,请稍等!!!!!!!!',
nowrap : false,
striped : true,
fit : true,
pagination:true,
columns : [[
{field:'checkbox',checkbox:true},
{field : 'authorization_id',title : '编号',width : 100,align:'center'},
{field : 'sn',title : 'sn号',width : 120,align:'center'},
{field : 'applicantName',title : '申请人',width : 100,align:'center'},
{field : 'time',title : '时间',width : 120,align:'center'},
{field : 'address',title : '地区',width : 280,align:'center'}
]],
toolbar: [{
width:'50',
iconCls: 'icon-undo',
handler: function(){ExcelImport();}
},'-',{
width:'50',
iconCls: 'icon-redo',
handler: function(){ExcelExport();}
},'-',{
width:'50',
iconCls: 'icon-cancel',
handler: function(){ExcelExport();}
}]
});