• Struts2学习笔记(1)---相关配置


    Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。

    1创建action对象(三种)

    1 创建普通的类,不继承任何类,也不实现接口(不用)

    2 创建类实现接口Action

    public interface Action{
             public static final String ERROR="error";     
             public static final String SUCCESS="error";     
             public static final String INPUT="error";     
             public static final String LOGIN="error";     
             public static final String NONE="error";     
             public  String execute() throws Exception;           
    }
    

      从action接口可以看到,Action接口定义了五个字符串常量和一个execute方法,每个Action类都实现了

    execute方法,该方法返回一个字符串,而接口定义了五个常量用来统一返回值。

    public class UserAction extends Action {	
    	public String execute() throws Exception{
    		return SUCCESS;
    		
    	}
    

      

    3 创建类继承ActionSupport(重点)

    ActionSupport类实现了Action接口,默认的Action实现类,而且里面提供很多默认方法,包括数据校验,国际化信息等,所以一般程序猿定义Action类都会继承ActionSupport类

    public class UserAction extends ActionSupport {
    	
    	public String execute() throws Exception{
    		return SUCCESS;
    		
    	}
    

      创建Struts.xml配置文件(src目录下)

    引入约束

    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
    "http://struts.apache.org/dtds/struts-2.1.dtd">
    

    配置action

    	<package name="user" extends="struts-default" namespace="/">
    		<!-- name: 访问名称 -->
    		<action name="hello" class="cn.entily.action.UserAction" method="{1}">
    			<!-- 配置方法的返回值到页面,返回值为空不用写 -->
    			<result name="success">/hello.jsp</result>
    		</action>
    	</package>
    

    配置struts过滤器(web.xml)

    <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
    
    </filter-mapping>
    

      一般在公司开发中都是分模块开发,每个开发人员都要在struts.xml里面配置action,所以一般都同过写另外个xml文件配置action,然后在引入到struts.xml中。

    <!--引入xml文件 -->
    <include file="com/abc/action/hello.xml"/>
    

      struts.xml其他配置

    全局结果页面

    <global-results>
          <result name="success"> /hello.jsp</result>
    </global-results>
    

      result标签还有一个type属性

           type属性表示如何去路径

                   默认    转发    dispatcher

                   重定向       redirect

  • 相关阅读:
    CF1416D Graph and Queries
    Wordpress建站系统相关
    微观经济学
    Preface
    Thread pool in chromium
    [fllutter engine] 并发消息队列
    bugku misc
    python 3.1学习
    HTML&CSS
    DOM技术点
  • 原文地址:https://www.cnblogs.com/durui/p/7649889.html
Copyright © 2020-2023  润新知