RequestProcessor 类的扩展
当然那我想转移Action的配制文件那也可以的
struts_config.xml配制文件
package com.bbs.struts.exts;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ModuleConfig;
import com.dd.util.DBConnection;
public class ExtRequestProcessor extends RequestProcessor{
@Override
public void init(ActionServlet servlet, ModuleConfig moduleConfig)
throws ServletException {
//从上下文中获得数据源
Object obj = servlet.getServletContext().getAttribute("jdbc/sql");
DBConnection.initDataSource(obj);
super.init(servlet, moduleConfig);
}
/**
* 国际化问题解决 processLocale
*/
protected void processLocale(HttpServletRequest request,
HttpServletResponse response) {
}
/**
* 设置请求时的编码
* */
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
String contentType = this.moduleConfig.getControllerConfig().getContentType();
int index = contentType.lastIndexOf("=");
String encode = contentType.substring(index+1, contentType.length());
try {
request.setCharacterEncoding(encode);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
/**
* 设置响应的字符编码
* */
protected void processContent(HttpServletRequest request,HttpServletResponse response) {
String contentType = moduleConfig.getControllerConfig().getContentType();
response.setContentType(contentType);
}
/*
* 转移ActionForm的配制文件.
*
* */
Properties prop = null;
protected ActionForm processActionForm(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping) {
ActionForm instance = null;
System.out.println(mapping.getName());
if (prop == null) {
String path = this.getServletContext().getRealPath(
"/WEB-INF/file.properties");
prop = new Properties();
try {
InputStream ins = new FileInputStream(path);
prop.load(ins);
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
String formName = mapping.getName();
String className = prop.getProperty(formName);
try {
Object obj = Class.forName(className).newInstance();
if (obj != null) {
instance = (ActionForm) obj;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return (instance);
}
}
当然那我想转移Action的配制文件那也可以的
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/>
<!--这里的FormBean 写在另外一个file.properties 文件以key/value 型式书写-->
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/local" name="aa" type="com.dd.struts.action.DemoAction"></action>
</action-mappings>
<controller contentType="text/html;charset=GBK" processorClass="com.dd.struts.actions.ExtRequestProcessor"></controller>
<message-resources parameter="com.dd.struts.ApplicationResources" />
</struts-config>