zoukankan      html  css  js  c++  java
  • struts2 基础

    框架(frameWork):某一种应用的半成品
      struts2: 表现层 处理与页面进行交互的相关功能 
      hibernate: 持久层 负责业务逻辑数据的持久化 
      spring: 业务层 负责复杂的业务逻辑  
      mybatis: 持久层 负责业务逻辑数据的持久化 跟数据库做交互 

    struts1 与 struts2 的区别:
      1.struts2 是在webwork2的基础上发展而来的,他不依赖于struts API 和 servlet API
      2.struts2提供了拦截器,利用拦截器可以进行AOP(切面)编程
      3.struts2提供了类型转换器,可以使用转换器把参数转化成我们需要的类型
      4.struts2提供了多种表现层技术
      5.struts2的输入校验可以对指定的方法进行校验
      6.struts2提供了全局范围,包范围、action范围 的国际化资源文件管理
      ※struts2太灵活,不易控制


    搭建 struts2 的开发环境:
      1.导入jar包:找到所需的jar包:发行包的lib目录中

            

      2.在工程 src 下编写struts2的配置文件  struts2.xml

      

     1 <?xml version="1.0" encoding="UTF-8"?>
     2  <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">    
     5 <struts>
     6     <constant name="struts.devMode" value="true"></constant>
     7     
     8     <package name="ly" namespace="/" extends="struts-default">
     9         <action name="helloWorld" class="cn.gs.ly.HelloAction" method="sayHello">
    10             <result name="success" type="dispatcher">/1.jsp</result>
    11         </action>
    12     </package>
    13 </struts>
    View Code

         

       3. 配置核心控制器,就是一个过滤器  web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>Struts2</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12     
    13   <filter>
    14       <filter-name>struts2</filter-name>
    15       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    16   </filter>
    17   <filter-mapping>
    18       <filter-name>struts2</filter-name>
    19       <url-pattern>/*</url-pattern>
    20   </filter-mapping>
    21   
    22   
    23 </web-app>
    View Code

          

    如果TOmcat启动成功,没有报错,证明环境搭建成功!

    注解方式 

    1.导入asm等相关jar包

    2.web.xml 添加struts 2 核心过滤器

    3.src目录下新建com.yzpc.action包,并在该包下创建LoginAction类继承ActionSupport类

    开发一个Struts2案例

    1、编写struts.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2  <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">    
     5 <struts><!--这是Struts2配置文件的根元素-->
     6     <constant name="struts.devMode" value="true"></constant><!-- 指定容器自动加载   -->
     7     
     8     <package name="ly" namespace="/test" extends="struts-default">
     9                 <!--
    10                     pageckage:方便管理动作元素
    11                         name:必须有包的名称,配置文件中必须保证唯一。
    12                         namespace:该包的名称空间,一般是以"/"开头
    13                         extends:继承的父包的名称。struts-default名称的包是struts2框架已经命名好的一个包。(在struts2-core.jar的struts-default.xml中)
    14                         abstract:是否是抽象包。没有任何action元素的包就是抽象包(java类)
    15                 -->
    16         <action name="helloWorld" class="cn.gs.ly.HelloAction" method="sayHello">
    17                 <!--
    18                     action:代表一个请求动作
    19                         name:动作的名称,同包中必须唯一。
    20                         class:负责处理的JavaBean的类全名。动作所对应的处理类,包全名.类名
    21                         method:JavaBean类中的对应处理方法。(动作方法:特点是,public String 方法名(){})
    22                 -->
    23             <result name="success" type="dispatcher">/1.jsp</result>
    24                 <!--
    25                     result:结果类型
    26                         type:跳转的方式
    27                         name:动作方法返回的字符串
    28                         主体内容:跳转的具体地址
    29                 -->
    30         </action>
    31     </package>
    32 </struts>

    2. 根据配置文件,创建需要的javabean和对应的动作方法, 在动作方法中完成逻辑调用。

     1 package cn.gs.ly;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class HelloAction extends ActionSupport{
     6     private String message;
     7     
     8     public String getMessage() {
     9         return message;
    10     }
    11 
    12     public void setMessage(String message) {
    13         this.message = message;
    14     }
    15 
    16     public String sayHello(){
    17         message = "hello nice to meet you!";
    18         return "success";
    19     }
    20     
    21     
    22     @Override
    23     public String execute() throws Exception {
    24         // TODO Auto-generated method stub
    25         return "success";
    26     }
    27     
    28 }

    ※struts 2  中预定义的ResultType

    3、编写网页文件,显示结果

     1 <%@ page language="java" contentType="text/html"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5     <head>
     6         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7         <title>Insert title here</title>
     8     </head>
     9     <body>
    10         ${message} 
    11         <h1>1.jsp</h1>
    12     </body>
    13 </html>

    4、访问helloworld动作的方式:

    http://localhost:8080/struts2/test/helloworld 应用名称/包的名称空间/动作的名称

    搜索顺序

    1 http://localhost:8080/struts2day01/test/a/b/c/helloworld
    2             /test/a/b/c:名称空间
    3             helloworld:动作名称
    4 
    5     搜索顺序:名称空间
    6             /test/a/b/c  没有helloworld
    7             /test/a/b    没有helloworld
    8             /test/a      没有helloworld
    9             /test        有了,调用执行
    struts.xml 中的各项默认值
        package如果没有namespace 默认namespace="/";
        action如果没有class,就会找    com.opensymphony.xwork2.ActionSupport
        action如果没有method,就会找   public String execute(){}    
        
        result name的常量值:
            SUCCESS:success;  
            NONE:none;  
            ERROR:error;  
            INPUT:input;  
            LOGIN:login;                
        result如果没有 type,默认是dispatcher    
            <result-types>
                <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
                <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
                <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
                <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
                <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
                <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
                <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
                <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
                <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
                <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
                <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
            </result-types>
            
            dispatcher:普通转发到某个页面        
            chain:普通抓发到某个动作
            redirect:重定向到某一页面
            redirectAction:重定向到某一动作
            plainText:以纯文本的形式输出jsp的内容
            
    动态设置action属性
         <action name="helloWorld6" class="cn.gs.wwg.HelloAction" >
                    <param name="message">i am best cool</param>
                    <result name="success" >/5.jsp</result>
        </action>
    取得action属性
        <result name="success" type="dispatcher">/6.jsp?msg=${message}</result>
        ---6.jsp中取值 :  ${param.msg}
    
    控制action的后缀名:指定需要struts2处理的请求后缀
        <constant name="struts.action.extension" value="do"></constant>
    开发中配置文件的更改,在访问时让框架自动重新加载:指定容器自动加载
        <constant name="struts.devMode" value="true"></constant>

    各种result使用示例

     1 <?xml version="1.0" encoding="UTF-8"?>
     2  <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">    
     5 <struts>
     6     <constant name="struts.devMode" value="true"></constant>
     7     <constant name="struts.action.extension" value="do,,action"></constant>
     8     
     9     <!-- result 元素写法,方式一。  -->
    10     <package name="ly" namespace="/" extends="struts-default">
    11         <action name="helloWorld" class="cn.gs.ly.HelloAction" method="sayHello">
    12             <result name="success" type="dispatcher">/1.jsp</result>
    13         </action>
    14     </package>
    15     
    16     <!-- result 元素写法,方式二。 -->
    17     <package name="ly2" namespace="/test2" extends="struts-default">
    18         <action name="helloWorld2" class="cn.gs.ly.HelloAction" method="sayHello">
    19             <result name="success" type="chain">helloWorld21</result>
    20         </action>
    21         <action name="helloWorld21">
    22             <result name="success" type="dispatcher">/2.jsp</result>
    23         </action>
    24     </package>
    25     
    26     <package name="ly3" namespace="/test3" extends="struts-default">
    27         <action name="helloWorld3" class="cn.gs.ly.HelloAction" method="sayHello">
    28             <result name="success" type="redirectAction">
    29                 <param name="actionName">helloWorld31</param>
    30                 <param name="namespace">/test31</param>
    31             </result>
    32         </action>
    33     </package>
    34     <package name="ly31" namespace="/test31" extends="struts-default">
    35         <action name="helloWorld31">
    36             <result name="success" type="dispatcher">/3.jsp</result>
    37         </action>
    38     </package>
    39         
    40     <package name="ly4" namespace="/test4" extends="struts-default">
    41         <action name="helloWorld4" >
    42             <result name="success" type="plainText">/4.jsp</result>
    43         </action>
    44     </package>
    45     
    46     <!-- 动态的给Action类的属性赋值  -->
    47     <package name="ly5" namespace="/test5" extends="struts-default">
    48         <action name="helloWorld5" class="cn.gs.ly.HelloAction">
    49             <param name="message">what is your name</param>
    50             <result name="success" >/5.jsp</result>
    51         </action>
    52     </package>
    53     
    54     <!-- 获取action的值 -->     
    55     
    56     <package name="ly6" namespace="/test6" extends="struts-default">
    57         <action name="helloWorld6" class="cn.gs.ly.HelloAction" method="sayHello">
    58             <result name="success" type="dispatcher">/6.jsp?msg=${message}</result>
    59         </action>
    60     </package>
    61          
    62 
    63 </struts>
    64     
  • 相关阅读:
    C语言考点例题解析
    五笔打字
    常用快捷键
    网络基础知识
    人口增长
    8 封装
    9 绑定方法和非绑定方法
    6 抽象类
    7 多态和多态性
    5 组合
  • 原文地址:https://www.cnblogs.com/liuyangv/p/8330465.html
Copyright © 2011-2022 走看看