Struts2基础01

1.Struts2概述

  1. struts2框架应用 javaee三层结构中web层框架
  2. struts2框架在 struts1和 webwork基础之上发展全新的框架
  3. struts2解决间题:
   4. structs2版本
   5. web层常见框架 : springMVC
 

2.Structs2框架入门

1.导入jar包

Maven方式:
pom.xml配置依赖:
<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.16</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts.xwork</groupId>
        <artifactId>xwork-core</artifactId>
        <version>2.3.34</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

 

2.创建action

 

3.配置action类访问路径

    1).配置Struts2核心配置文件

        名称: src下  位置: struts.xml(  固定 

    2).引入dtd约束

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

    3). 配置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>Struct2day1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
         org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class><!-- 低版本对应的class可能有细微差别 -->
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

4.Struts2基本流程

 

5.Struts2配置

    Struts2核心配置文件 -> struts.xml
    结构:
    <package>
    <action>
        <result> </result>
    </action>
    </package>

     1) package 标签

            类似于代码中的包,用于区分不同的action
            三个属性:
      • name : name属性值和功能本身没有关系,在一个配置文件中可以写多个package标签,name属性值是不能相同的
      • extends:属性值是固定的 ‘ struts-default ’ ;写了合格属性后,在package里配置的类具有action功能
      • namespace  :name属性值要和action中的name属性值构成访问路径    默认值'/'             

    2)action标签

            action标签配置action访问路径
            action标签属性:
      • name : name属性值要和action中的name属性值构成访问路径,package里可以有多个action,但是他们的name值不能相同
      • class : action全路径: 包名+类名
      • method : 比如在 action里面默认执行的方法 execute方法,但是在 action里面写其他的方法,让 action里面多个方法执行,使用 method进行配置               

    3)result标签

            根据action返回值,配置到不同页面中去
            result标签属性
      • name :  和方法返回值一样
      • type : 配置如何到页面中去(转发、重定向) 默认转发  

    4)常量配置

 
 
 

6.分模块开发

    单独写配置文件,把配置文件引入到核心配置文件中
    <include file="per/wzy/Action/hello.xml"></include> <!--per/wzy/Action => 包名 -->

7.Action编写方式

    • 第一种 : 创建普通类 ,不继承任何类、不实现任何接口
    • 第二种 : 创建类 ,实现接口Action
    • 第三种 : 创建类 , 继承类ActionSupport  (常用)   
 

8.访问Action的方法

 访问action有三种方式
    • 一:在action中添加method属性,填写要访问的方法    (缺陷:每个action都需要配置,若action里有多个方法需要执行,则需要配置大量action)
<action name="hello" class = "per.wzy.Action.Hello" method="add">
     <!-- 配置方法的返回值到不同的页面 -->
     <!--  <result name ="ok">/Hello.jsp</result>-->
</action>
    • 二:使用通配符实现,在action中的name属性中添加 * 通配符,匹配任意字符 {第几个*} 代表*替代的值
                      注: s truts2 2.5后禁用通配符,需要在package 中添加 strict-method-invocation="false"
<struts>
    <package name="" extends="struts-default" strict-method-invocation="false">
        <action name="stu_*" class="per.wzy.Action.Hello" method="{1}">
            <allowed-methods></allowed-methods>
        </action>
    </package> 
</struts>
            访问http://localhost:8080/Struct2day1/stu_add和http://localhost:8080/Struct2day1/stu_del分别执行add()方法和del()方法
    • 三:动态访问实现

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





猜你喜欢

转载自www.cnblogs.com/local-wzy/p/11229571.html