Struts1.2中多config的实现

本文详细介绍了如何在Struts框架中创建自定义请求处理器,包括处理创建Action、填充form、预处理请求的功能,并通过配置web.xml和struts-config.xml文件实现与框架的集成。此外,文章还展示了如何在index.jsp页面使用此处理器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自http://hi.baidu.com/shiryu963

 

在com.shiryu中新建MyRequestProcesser.java

package com.shiryu;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;

public class MyRequestProcesser extends RequestProcessor {

    @Override
    protected Action processActionCreate(HttpServletRequest request,
            HttpServletResponse response, ActionMapping mapping)
            throws IOException {
        // TODO Auto-generated method stub
        System.out.print("-----------创建Action-----------");
        return super.processActionCreate(request, response, mapping);
    }

    @Override
    protected ActionForm processActionForm(HttpServletRequest request,
            HttpServletResponse response, ActionMapping mapping) {
        // TODO Auto-generated method stub
        System.out.print("-----------填充form-----------");
        return super.processActionForm(request, response, mapping);
    }

    @Override
    protected boolean processPreprocess(HttpServletRequest request,
            HttpServletResponse response) {
        // TODO Auto-generated method stub
        System.out.print("-----------预处理请求-----------");
        String address = request.getRemoteAddr();
        int position = address.lastIndexOf(".");
        String last = address.substring(position + 1);
        int lastNum = Integer.parseInt(last);
       
        if (address.substring(0, 9).equals("125.221.33")
                && (lastNum <= 151 && lastNum > 0)) {
            System.out.print(address+lastNum);
            return true;
        }
        if(address.equals("127.0.0.1")){
            System.out.print(address+lastNum);
            return true;
        }else {
            return false;
        }
    }
}

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>config/shiryu</param-name>
      <param-value>/WEB-INF/struts-config1.xml</param-value>
    </init-param>

   
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

在web-inf 新建并配置 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 />
<global-exceptions />
<global-forwards />
<action-mappings>
    <action
           path="/test"
           type="org.apache.struts.actions.SwitchAction">
    </action>

</action-mappings>
<message-resources parameter="com.shiryu.ApplicationResources" />

<controller processorClass="com.shiryu.MyRequestProcesser"></controller>
</struts-config>

配置struts-config1.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 />
<global-exceptions />
<global-forwards />
<action-mappings>
   <action path="/wel" forward="/wel.jsp"></action>
</action-mappings>
<message-resources parameter="com.shiryu.ApplicationResources" />
</struts-config>

新建index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
    This is my JSP page. <br>
    <a href="test.do?prefix=/shiryu&page=/wel.do">zhaiyu</a>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值