Struts2(1)

  一、struts概述

  1、struts2框架应用在javaee三层结构中web层框架

  2、struts2框架在struts1和webwork基础之上发展全新的框架

二、struts2环境搭建

  1、导包

    

  2、创建action

/*
 * 创建action有3种方式
 * 方式一:创建普通类,不继承任何类,不现实任何接口
 * 方式二:继承ActionSupport类(开发一般使用这种方式)
 * 方式三:实现Action接口
 */

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

//方式一
public class Aaction {
    
    /* 1、每次发访问servlet时候,都会执行service方法
     * 2、访问action时,默认执行execute方法 
     */
    public String execute(){
    /*action里面的方法可以没有返回值,没有返回值时候,在result标签不需要配置
      - 把方法写成void
      - 让返回值,返回 ”none”*/
return "ok"; } } /*方式二 public class Baction extends ActionSupport { public String execute(){ return NONE; } } */ /* * 方式三 * public class Caction implements Action { @Override public String execute() throws Exception { return SUCCESS; } } */

  3、配置访问路径

    

第三步 配置action类访问路径

  (1)创建struts2核心配置文件

      - 核心配置文件名称和位置是固定的

      - 位置必须在src下面,名称 struts.xml

 

  (2)引入dtd约束

         

   (3)配置action

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    <!-- package标签:类似与包,区别不同的action,必须首先写package
                    在package里面才能配置action
        package属性:
                    name属性:属性值跟功能本身没有关系,可以任意写
                            在一个配置文件中,可以写多个package标签,name的属性值不能相同
                    extends属性:固定值: struts-default
                            写了这个属性后,在package里面配置的类具有action功能
                    namespace属性:属性值与action标签里面的name属性值构成访问路
                     -->
        <package name="hello" extends="struts-default" namespace="/">
        <!-- action标签配置action的访问路径
                name属性:属性值可以任意写,属性值与package标签里面的namespace标签的属性值构成访问路径
                        在package标签里面可以写多个action标签,但是action的name属性之不能相同
                class属性: action类的全路径        
                method属性:不写表示默认执行execute方法,要执行其他方法时,直接写其他方法的方法名
                - 比如在action里面默认执行的方法execute方法,但是在action里面写其他的方法
                - 让action里面多个方法执行,使用method进行配置    
         -->
            <action name="hello">
            <!-- result标签:根据action的返回值,配置到不同的路径里面
                 name属性:和方法返回值一样
                 type属性:配置如何到路径中去(2种方式:转发或者重定向)
                          默认转换
             -->
                <result></result>
            </action>
        </package>
    </struts>

   (4)配置过滤器(在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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>struts2</display-name>
  
  <!--Struts2过滤器-->
   <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>
  <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>
</web-app>

  

原文地址:https://www.cnblogs.com/flei/p/6629483.html