struts框架-Servlet

一、基本配置
1.创建struts.xml配置文件
<?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">

这里写图片描述

<?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 name 标签,随便写,只要不重复就行
    可以进行模块区分
      namespace:命名空间(访问Action网址前的一个网址部分)
      写东西也可以直接/
      写东西的话,访问时,要在Action类前面加上所写的东西
      www.baidu.com/tieba
                   /wenku
                   /baike
         extends:继承一个包struts-default,默认的配置,默认提供的功能     
      -->
      <!--
       action
       name 是你这个类的访问的路径
       class:类的全限定类名
       method :类方法被访问的方法

       result 方法的返回值(进行匹配)
       type 请求跳转的方式,默认的是请求转发dispatcher(不用写)
       值得部分:跳转网站资源

        -->


  <struts>
    <package name="hello" namespace="/" extends="struts-default">
      <action name="HelloAction" class="com.lanou3g.hello.HelloAction" method="hello">
      <!--type="dispatcher"跳转方式  -->
        <result name="success" type="dispatcher">/hello.jsp</result>
      </action>

    </package>

  </struts>

public class HelloAction {

    /*
     * struts2初体验
     * 
     * 执行流程
     * 1.通过网址请求中的hello
     * /hello
     * 2.找对应的命名空间(网址)
     * 3.找到后,再通过网址中的HelloAction去匹配
     * Action标签中name
     * 4匹配上,用class标签,创建其类的对象
     * 5,调用该类中的方法
     * 6.拿到类中方法的返回值,去匹配,result标签
     * 7.返回值匹配上,调用标签中的页面
     * 
     *http://localhost:8080/sh-struts-01/hello/HelloAction
     */
      public String hello() {
          System.out.println("Hello Struts!");
          return "success";
      }
}
二、常量配置,标签

这里写图片描述

<?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>
  <!--常量配置修改  -->
  <!--去default.properties文件 找到你要修改的配置  -->
  <!--name:键值 value:值  -->
  <constant name="struts.i18n.encoding=UTF-8" value="UTF-8"></constant>
  <!-- 
  struts.action.extension=action,,
  action,,
  表示,访问路径的后缀名,可以是.action获取无后缀
   -->
  <constant name="struts.action.extension" value="action,,"></constant>

  <!--struts.devMode = false
  可以给你的配置文件提供,热加载(更改完了不用重启服务器)
    -->
    <constant name="struts.devMode" value="true"></constant>
    <package name="def" namespace="/def" extends="struts-default">
      <action name="Demo01Action" class="com.lanou3g.def.Demo01Action" method="defaultFun">
      <!--type="dispatcher"跳转方式  -->
        <result name="success" type="dispatcher">/hello.jsp</result>
      </action>

    </package>
    <!--引入其他的struts配置文件  -->

  </struts>

package com.lanou3g.def;

public class Demo01Action {
  public String defaultFun() {
      System.out.println("Demo01Action");
      return "success";
  }
}
三、常量配置,动态方法标签
<?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.enable.DynamicMethodInvocation = false 
      默认动态方法是关闭的  -->
      <!--!dynamic/Demo02Action_add.action  -->
      <!-- 使用通配符,配置访问路径
        *是你的方法名,
        method标签,中{1}代表取到前面星号 获取的方法名
       -->
  <struts>

   <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>


    <package name="dynamic" namespace="/dynamic" extends="struts-default">
    <!--dynamic/Demo02Action_add.action  -->
      <action name="Demo02Action_*" class="com.lanou.dynamic.Demo02Action" method="{1}" > 
      <!--type="dispatcher"跳转方式  -->
        <result name="success" >/hello.jsp</result>
      </action>

    </package>

  </struts>

/*
 * 动态方法调用测试
 */
public class Demo02Action {

     public String add() {
         System.out.println("增加方法");
         return "success";
     }
     public String delete() {
         System.out.println("删除方法");
         return "success";
     }
     public String update() {
         System.out.println("修改方法");
         return "success";
     }
     public String find() {
         System.out.println("查找方法");
         return "success";
     }
}
四、web.xml中filter配置文件(核心)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>sh-struts-01</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>

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值