RequestProcessor 类的扩展

RequestProcessor 类的扩展


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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值