zoukankan      html  css  js  c++  java
  • Struts1简单开发流程梳理

    共享数据的4种范围
    MVC设计模式
    JSP model1、JSP model2
    struts实现MVC机制(ActionServlet、Action)
    struts-config.xml

    ActionServlet、ActionForm、Action、ActionMapping、ActionForward
    struts应用、servlet容器、jsp容器、Java web容器

    struts入门应用:helloapp
    1.创建视图组件
    hello.jsp、HelloForm Bean
    创建jsp文件(导入加载struts标签库)<%@ taglib uri=""%>
    输出到网页上的文本内容都是由<bean:message key="hello.jsp.prompt.person"/>标签生成的
    <bean:write name="" property=""/>
    <html:errors>、<html:form>(html表单字段与ActionFormBean关联)、<html:text>
    <logic:present>
    2.创建消息资源文件ResourceBundle
    <bean:message key="hello.jsp.prompt.person"/>
    application.properties(例如:hello.jsp.title=Hello!)
    3.创建ActionForm Bean
    当用户提交HTML表单之后,struts框架自动把表单数据组装到ActionForm Bean中
    ActionForm Bean中的属性和HTML表单中的字段一一对应
    先继承ActionForm抽象类比Java bean多了重置默认值reset和数据验证validate方法 ActionErrors
    4.数据验证
    表单验证(ActionForm Bean中)、业务逻辑验证(Action Bean中)

    提交表单后,struts自动把数据组装到ActionForm Bean中,框架调用ActionForm Bean的validate方法进行
    表单验证,若返回Action Errors对象为null或者不包含任何ActionMessage对象,就表示没有错误,通过。
    struts1.2废弃Action Errors对象,采用ActionMessage
    5.创建控制器组件
    包括ActionServlet类和Action类
    ActionServlet类是struts框架自带的,是整个框架的控制枢纽,通常不需要扩展
    Action类是可扩展的,用来处理特定的Http请求(继承Action类)
    A.Action类的工作机制
    继承Action类,覆盖execute()方法当ActionForm Bean被创建并表单验证通过后,struts框架就会调用Action类
    的execute()方法。该方法返回ActionForward对象,该对象包含了请求转发路径信息。
    execute方法参数: ActionMapping包含这个Action的配置信息和struts-config.xml文件中的<action>元素对应
             ActionForm 包含用户的表单数据(ActionForm数据通过验证才会调用execute方法)
            HttpServletRequest
            HttpServletResponse
    B.访问封装在MessageResources中的本地化文本
    Action类中定义了getResources(HttpServletRequest req)方法,返回默认的MessageResources对象,它封装了Resource Bundle中
    的文本内容。调用MessageResources对象(已经调用getResources方法返回的对象)messages.getMessage("hello.jsp.title")获取内容
    C.业务逻辑验证
    Action类的execute方法执行业务逻辑验证(ActionMessages对象的add方法)
    (String)((HelloForm)form).getUserName();
    Action类自带方法saveErrors(request,errors);<html:errors>
    ActionErrors继承ActionMessages
    ActionMessages与ActionMessage是聚集关系 1个包含多个
    ActionError继承ActionMessage
    6.访问模型组件
    创建PersonBean对象并调用方法(Action类一般会调用访问模型组件)
    7.向视图组件传递数据
    Action类把数据存放在request或session范围内,以便向视图组件传递数据
    request.setAttribute(Constants.PERSON_KEY,pb);
    request.removeAttribute(mapping.getAttribute());
    8.把HTTP请求转发给合适的视图组件
    Action类把流程转发给合适的视图组件
    return mapping.findForward("SayHello"); 返回ActionForward对象

    9.创建模型组件JavaBean、EJB

    10.创建存放常量的Java文件(request对象setAttribute以及getAttribute需要一个key值)
    struts应用提倡将这些属性key常量定义在一个Java文件的Constants.java文件中
    public final class Constants{ public static final String PERSON_KEY="personbean";}
    使用Constants.PERSON_KEY

    11.创建配置文件
    创建web应用的配置文件
    web.xml对ActionServlet类进行配置
    声明web应用使用的Struts标签库(例如:Struts Bean、Struts Html、Struts Logic标签)
    创建Struts框架的配置文件 struts-config.xml
    <struts-config>、<form-beans>、<form-bean>、<action-mappings>、<action>、<forward>
    <message-resources>
    分析:<form-bean name="HelloForm" type="hello.HelloForm"/>
    配置了一个Action Bean,名为HelloForm 类为hello.HelloForm
    <action path="/HelloWord"
    type="hello.HelloAction"
    name="HelloForm"
    scope="request"
    validate="true"
    input="/hello.jsp"
    >
    <forward name="SayHello" path="/hello.jsp"/>
    </action>
    配置Action组件,path请求访问Action路径,type属性指定Action的完整类名,
    name指定需要传递给Action的ActionForm Bean,scope指定ActionForm Bean的存放范围
    validate指定是否执行表单验证,input指定当表单验证失败时的转发路径
    <forward>子元素定义请求转发路径

    链接 /sysNotifyTodo.do?method=view 配置文件的action标签parameter="method"
    指定调用action哪个方法,此时是view方法

    <message-resources parameter="hello.application"/>定义了一个Resource Bundle
    parameter属性指定Resource Bundle使用的消息资源文件,此时消息资源文件名为application.properties
    存放路径为WEB-INF/classes/hello/application.properties
    可以在web.xml或struts.xml配置,其中key属性不是必要的
    <message-resources parameter="application" key="local" />
    可在页面用struts标签引用资源或在Action类中调用
    MessageResources messageResources = this.getResources(request, "local");
    String one = messageResources.getMessage("one");

    12.发布和运行应用
    作为Java Web应用,目录结构应符合Sun公司规范。导包jar文件以及标签库描述文件tld文件。

    页面的链接路径要去掉.do才和struts.xml配置文件的action的path路径属性匹配
    注意一点,如果自定义的ActionClass中重写了excute方法,那么即使指定了method,
    所有的请求还是会走excute方法,而不是指定的doAjax方法。
    (页面的原生的html标签链接路径前面不需要斜杆/,而struts.xml配置文件需要/)

    web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>myweb</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>/office/door.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <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.xml</param-value>
          </init-param>
      </servlet>
      <servlet-mapping>
          <servlet-name>action</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    </web-app>

    struts.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
    <struts-config>
    
        <!-- 表单 Bean -->
        <form-beans>
            <form-bean name="door" type="forms.Door" />
        </form-beans>
    
        <!-- 操作映射 -->
        <action-mappings>
    
            <action 
                path="/doors/entrance"
    
                
                type="actions.DoorAction"
                parameter="method"
                scope="request"
                name="door"
                validate="true"
                input="/error.jsp">
                
                <forward name="welcome"
                    path="/office/welcome.jsp" />
            </action>
    
        </action-mappings>
    
        <message-resources parameter="application"
            key="local" />
    </struts-config>

    door.jsp文件

    <body>
        <form action="doors/entrance.do">
            门名:<input type="text" name="dname"/>
            高度:<input type="text" name="height">
            <input type="hidden" name="method" value="myMethod"/>
            <input type="submit" value="提交"/>
        </form>
    </body>

    DoorAction.java文件

     1 package actions;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 
     6 import org.apache.struts.action.ActionForm;
     7 import org.apache.struts.action.ActionForward;
     8 import org.apache.struts.action.ActionMapping;
     9 import org.apache.struts.actions.DispatchAction;
    10 import org.apache.struts.util.MessageResources;
    11 
    12 public class DoorAction extends DispatchAction {
    13 
    14 //    @Override
    15 //    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    16 //            HttpServletResponse response) throws Exception {
    17 //        System.out.println("door execute action");
    18 //        return mapping.findForward("welcome");
    19 //    }
    20     public ActionForward myMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    21             HttpServletResponse response) throws Exception {
    22         System.out.println("door myMethod action");
    23         String heightstr = request.getParameter("height");
    24         int height = Integer.parseInt(heightstr);
    25         MessageResources messageResources = this.getResources(request, "local");
    26         String one = messageResources.getMessage("one");
    27         String two = messageResources.getMessage("two");
    28         String three = messageResources.getMessage("three");
    29         System.out.println(one+two+three);
    30         if(height>=100){
    31             System.out.println("高度大于等于100!");
    32         }else{
    33             System.out.println("高度小于100!");
    34         }
    35         request.setAttribute("dname", request.getParameter("dname"));
    36         request.setAttribute("height", height);
    37         return mapping.findForward("welcome");
    38     }
    39 
    40 }

    Door.java文件

     1 package forms;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 
     5 import org.apache.struts.action.ActionError;
     6 import org.apache.struts.action.ActionErrors;
     7 import org.apache.struts.action.ActionForm;
     8 import org.apache.struts.action.ActionMapping;
     9 import org.apache.struts.action.ActionMessage;
    10 import org.apache.struts.action.ActionMessages;
    11 
    12 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMessages;
    13 
    14 public class Door extends ActionForm {
    15 
    16     /**
    17      * 
    18      */
    19     private static final long serialVersionUID = 8910086866815354113L;
    20 
    21     private String dname = null;
    22     private String height = null;
    23 
    24     public String getDname() {
    25         return dname;
    26     }
    27 
    28     public void setDname(String dname) {
    29         this.dname = dname;
    30     }
    31 
    32     public String getHeight() {
    33         return height;
    34     }
    35 
    36     public void setHeight(String height) {
    37         this.height = height;
    38     }
    39 
    40     @Override
    41     public void reset(ActionMapping mapping, HttpServletRequest request) {
    42         // TODO Auto-generated method stub
    43         super.reset(mapping, request);
    44     }
    45 
    46     @Override
    47     public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    48         System.out.println("validate method!");
    49         System.out.println(dname + ":" + height);
    50         if (dname.trim().equals("") || height.trim().equals("")) {
    51             System.out.println("空!!!");
    52             ActionErrors actionErrors = new ActionErrors();
    53             actionErrors.add("error", new ActionMessage("error", "no!!!") );
    54 //            ActionMessages actionMessages = new ActionMessages();
    55 //            actionMessages.add("error", new ActionMessage("error", "no!!!"));
    56 //            ActionMessage actionMessage = new ActionMessage("error", "no!!!");
    57             return actionErrors;
    58         }
    59         // return super.validate(mapping, request);
    60         return null;
    61     }
    62 
    63 }
  • 相关阅读:
    .net mvc 路由
    Dos小技巧-在Dos中直接打开软件
    Dos操作基础
    使用uiautomator时遇到问题的处理方法
    3.UiObejct API 详细介绍
    2.UiSelector API 详细介绍
    腾讯加固纯手工简易脱壳教程
    手脱nSPack 3.7
    Servlet各版本web.xml的头文件配置模板
    dynamic web module 版本之间的区别
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/8839372.html
Copyright © 2011-2022 走看看