1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!
2). 如何进行整合 ?
①. 正常加入 Struts2
②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action
注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype
<bean id="personAction"
class="com.atguigu.spring.struts2.actions.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id
<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>
④. 加入 struts2-spring-plugin-2.3.15.3.jar
3). 整合原理: 通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中
获取 Action 的实例.
if (appContext.containsBean(beanName)) {
o = appContext.getBean(beanName);
} else {
Class beanClazz = getClassInstance(beanName);
o = buildBean(beanClazz, extraContext);
}
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>upload_test</title>
</head>
<body>
<s:form action="upload" method="post" theme="simple" enctype="multipart/form-data">
输入帐号:<s:textfield name="uid"/><br>
选择头像:<s:file name="headImage"/><br>
<s:submit value="提交"/>
<!--显示上传错误提示信息-->
<s:fielderror/>
</s:form>
</body>
</html>
package action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.IOException;
public class UploadAction extends ActionSupport {
private String uid; // 封装帐号(uid)请求参数属性
private File headImage; // 封装上传文件域属性
private String headImageContentType; // 封装上传文件类型的属性
private String headImageFileName; // 封装上传文件属性
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public File getHeadImage() {
return headImage;
}
public void setHeadImage(File headImage) {
this.headImage = headImage;
}
public String getHeadImageContentType() {
return headImageContentType;
}
public void setHeadImageContentType(String headImageContentType) {
this.headImageContentType = headImageContentType;
}
public String getHeadImageFileName() {
return headImageFileName;
}
public void setHeadImageFileName(String headImageFileName) {
this.headImageFileName = headImageFileName;
}
public String execute() throws IOException {
// 上传文件的保存位置在“/image”,该位置在tomcat服务器的“webapps”之中
String realpath= ServletActionContext.getServletContext().getRealPath("/image");
// 声明文件目录image,如果文件名不存在就建一个呗~
File file = new File(realpath);
if(!file.exists()){
file.mkdirs();
}
// 实现文件上传,也就是做了一个方法调用~
FileUtils.copyFile(headImage,new File(file,headImageFileName));
return SUCCESS;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--指定国际化资源文件(下一章会讲到)-->
<constant name="struts.custom.i18n.resources" value="messageResource"/>
<!--设置Struts应用的解码集-->
<constant name="struts.i18n.encoding" value="utf-8"/>
<!-- --- 包配置 ---- -->
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="action.UploadAction">
<result>/uploadSuccess.jsp</result>
</action>
</package>
</struts>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传初始页</title>
</head>
<body>
上传成功!<br>
<!--输入表单里的用户帐号属性-->
用户帐号:<s:property value="uid"/><br>
<!--根据上传文件名字,显示上传头像-->
您的头像:<img src="<s:property value="'image/' + headImageFileName"/> " alt="图像无法显示"/>
</body>
</html>
package com.model;
public class User {
//name属性
private String name;
//age属性
private int age;
//tel属性
private String tel;
//各属性的getter和setter()方法
}
package com.action;
import java.util.List;
import com.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class ListAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//users属性,为List类型
private List<User> users;
//getter方法
public List<User> getUsers() {
return users;
}
//setter方法
public void setUsers(List<User> users) {
this.users = users;
}
//重载execute()方法
public String execute() throws Exception {
for (User user : users) {
System.out.println("Name:"+user.getName()+" Age:"+user.getAge()+" Tel:"+user.getTel());
}
return SUCCESS;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- default包-->
<package name="default" namespace="/" extends="struts-default">
<!-- list Action返回success.jsp页面 -->
<action name="list" class="com.action.ListAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
<%@ 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>
<center>
<!-- 表单 -->
<s:form action="list">
<!-- 表格 -->
<table >
<tr>
<td></td>
<td>first:</td>
<td>second:</td>
</tr>
<tr>
<td>Name:</td>
<!-- 设置list元素-->
<td><s:textfield name="users[0].name" theme="simple"/></td>
<td><s:textfield name="users[1].name" theme="simple"/></td>
</tr>
<tr>
<td>Age:</td>
<td><s:textfield name="users[0].age" theme="simple"/></td>
<td><s:textfield name="users[1].age" theme="simple"/></td>
</tr>
<tr>
<td>Tel:</td>
<td><s:textfield name="users[0].tel" theme="simple"/></td>
<td><s:textfield name="users[1].tel" theme="simple"/></td>
</tr>
<tr>
<td colspan="3">
<s:submit></s:submit>
</td>
</tr>
</table>
</s:form>
</center>
</body>
</html>
<%@ 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>
<center>
Name:<s:property value="users[0].name"/>
Age:<s:property value="users[0].age"/>
Tel:<s:property value="users[0].tel"/><br/>
Name:<s:property value="users[1].name"/>
Age:<s:property value="users[1].age"/>
Tel:<s:property value="users[1].tel"/>
</center>
</body>
</html>