Struts 1.2 Action 开发流程
struts 1.2 版本开发
1.新建web project项目;
2.导入Struts 1.2插件包;
3.修改配置文件:
<?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> <form-beans> <form-bean name="tf" type=""></form-bean> </form-beans> <action-mappings>
<action path="/helloworld"
name="helloform"
type="com.hgihcom.helloAction.HelloAction"
scope="request"
input="/hello.jsp"
validate="true">
<forward name="success" path="/tosayhello.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.wonder.struts.ApplicationResources" />
</struts-config>
4.新建ActionForm extends ActionForm;
- 添加form属性及setter/getter;
- 添加默认文本框值方法:
public void reset(ActionMapping mapping, HttpServletRequest request) { this.userName="输入用户名。。。!";//初始化变量 }
- 添加验证方法:
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if(userName==null || userName.equals("")) { errors.add("username", new ActionMessage("hello.username.error")); } return errors; }
- 完成ActionForm配置;
5.创建业务界面:Action的触发页面和返回页面,创建sturts标签,要导入标签库:
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
6.创建业务处理DelAction extends Action,到配置文件,以视图方式创建,指定form(提交的表单),parameter(参数),
methods(处理方法名),forwards(响应视图),exceptions(外抛异常),实现处理方法;配置结果例如:
<?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> <form-beans> <form-bean name="helloform" type="com.hgihcom.helloActionForm.HelloActionForm"></form-bean> </form-beans> <action-mappings> <action path="/helloworld" name="helloform" type="com.hgihcom.helloAction.HelloAction" scope="request" input="/hello.jsp" validate="true" > <forward name="success" path="/tosayhello.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.highcom.struts.ApplicationResources" /> </struts-config>
7.关于配置文件:
- ApplicationResources.properties:可以配置message信息,输入提示信息,以及文本框的默认内容等等(直接用编辑器写,不用担心编码!);
- struts-config.xml:可以对Struts1的实现方案进行详细的配置设置;
- web.xml也有好多东东是在这里需要配置的,如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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> <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>