方式一:使用 Spring 的 ActionSupport或DispachActionSupport 类整合 Structs
Action
package com.yz.actions.action;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.*;
import org.springframework.web.struts.DispatchActionSupport;
import com.yz.services.*;
import com.yz.actions.form.*;
import com.yz.vo.UserVo;
//spring的action管理请求
public class UserAction extends DispatchActionSupport {
//针对接口编程
IUserServices ius;
public IUserServices getIus() {
return ius;
}
public void setIus(IUserServices ius) {
this.ius = ius;
}
public ActionForward test(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
UserVo uv = new UserVo();
try {
BeanUtils.copyProperties(uv, userForm);
} catch (Exception e) {
e.printStackTrace();
}
/**//*IUserServices ius = new IUserServicesImpl();*/
//通过父类的方法获取spring容器 并得到其中的bean对象
ius =(IUserServices)super.getWebApplicationContext().getBean("ius");
boolean add = ius.addUsers(uv);
System.out.println(uv.getUserName());
String url = "/error.jsp";
if(add) {
url = "/ok.jsp";
}
System.out.println(url);
return new ActionForward(url) ;
}
}
自己写的请求处理器,处理中文乱码
package com.yz.myReq;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
public class myReq extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
public class myReq extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="com.yz.actions.form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
name="userForm"
path="/user"
scope="request"
type="com.yz.actions.action.UserAction"
parameter="actions"
/>
</action-mappings>
<controller processorClass="com.yz.myReq.myReq"></controller>
<message-resources parameter="com.yz.actions.ApplicationResources" />
<!-- 添加一个插件,用来在启动struts框架的时候,
同时读取spring配置文件,启动spring容器 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="com.yz.actions.form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
name="userForm"
path="/user"
scope="request"
type="com.yz.actions.action.UserAction"
parameter="actions"
/>
</action-mappings>
<controller processorClass="com.yz.myReq.myReq"></controller>
<message-resources parameter="com.yz.actions.ApplicationResources" />
<!-- 添加一个插件,用来在启动struts框架的时候,
同时读取spring配置文件,启动spring容器 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>