简单写下Struts1配置过程:
1.导入必须的jar包,最少包括commons-beanutils.jar,common-digester.jar,common-logging.jar,struts.jar。
2.配置文件web.xml注册Struts1的servlet、配置文件名及路径、接收路径格式。
3.配置文件struts-config.xml,包括必要的formbeans(可无),action,forward。
4.写自己的action。
PS:文件如下:
1.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.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> </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>
2.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> <action-mappings> <action path = "/test" type = "com.tim.struts.action.TestAction"> <forward name = "success" path = "/index.jsp"></forward> </action> </action-mappings> </struts-config>
3.TestAction
package com.tim.struts.action; 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.ActionForward; import org.apache.struts.action.ActionMapping; public class TestAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("!!"); return mapping.findForward("success"); } }
4.对应jar包(struts1.2)
见附件。
5.访问路径,http://localhost:8888/工程名/test.do(也可配置域名访问)