• Struts2的常见配置&Action的访问


     struts2的概要

    什么是Struts2?

    • struts2是一个基于mvc的web层框架,本质上相当于一个servlet。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。
    • Spring MVC也是一个web层的框架

    web层框架基于前端控制器的设计

     struts2的入门例子

     创建web项目,导入需要的jar包,

    1 创建一个jsp界面

    <%@ page contentType="text/html;charset=UTF-8"  %>
    <html>
      <head>
        <title>hi</title>
      </head>
      <body>
      <h1>struts2的入门</h1>
      <h3><a href="${pageContext.request.contextPath}/hello.action">struts2的入门</a></h3>
      </body>
    </html>

    2 编写一个HelloAction的类

    public class helloAction {
       public String execute(){
           System.out.println("hello被执行了");
           return "sucess";
       }
    }

    3 配置struts.xml(在src目录下)

    <?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="a" extends="struts-default" namespace="/" >
    
        <action name="hello" class="cn.itcast.demo.helloAction">
            <result name="sucess" >/sucess.jsp</result>
        </action>
    </package>
    
    </struts>

    4 配置前端过滤器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <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>
    </web-app>

    5 跳转成功的界面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>sucess</title>
    </head>
    <body>
        <h1>恭喜!跳转成功</h1>
    </body>
    </html>

    struts2的执行流程

    当用户访问某一个action的时候,先经过过滤器,然后再到拦截器 (这组拦截器实现了框架中的部分功能)
    执行目标hello.action,在strtuts的配置文件中与hello对应上,根据name可以找到class类的全路径从而找到对应的
    Java类文件,后面根据反射执行。

    加载顺序
    注意:后配置的常量的值会覆盖先配置的常量的值

     常见配置

    1 配置文件的加载顺序

    2 package相关配置

       package标签称为包,这个包与Java中的包的概念不一致。包为了更好管理action的配置。

       package标签的属性

        name                 :包的名称,只有在一个项目中不重名即可。

       extends             :继承哪个包,通常值为struts-default。

       namespace       :名称空间,与<action>标签中的name属性共同决定访问路径。

       abstract            :抽象的,用于其他包的继承。

    3 action的相关配置

      action标签的属性

       name                 :与namespace共同决定访问路径

       class                  :Action类的全路径

       method             :执行Action中的哪个方法的方法名,默认值execute

       converter          :用于设置类型转换器

     Action的访问

              action类继承ActionSupport

    import com.opensymphony.xwork2.ActionSupport;
    
    public class demo3 extends ActionSupport {
        @Override
        public String execute() throws Exception {
            System.out.println("demo3执行了...");
            return NONE;
        }
    }

    通过通配符的方式进行配置(开发中这种方式用的最多)

       <!--通配符的配置-->
            <action name="product_*" class="cn.itcast.demo3.demo2" method="{1}"/>
    
    

     整合Hibernate和Struts2

    1搭建开发环境

      (1)新建一个web工程,引入jar包,

      (2)配置hibernate

        映射文件   核心配置文件   日志文件

      (3)配置Struts2

        struts.xml     web.xml

    2修改menu中列表的请求路径

     <TR> 
         <TD class=menuSmall><A class=style2 href="/crm/customer_find.action"
             target=main>- 客户列表</A></TD>

    </TR>

    编写Action类

    public class CustomerAction extends ActionSupport {
        public String find(){
            CustomerService CustomerService=new CustomerServiceimpl();
          List<Customer> list= CustomerService.find();
             ServletActionContext.getRequest().setAttribute("list",list);
            return "findSucess";
        }
    }

    此demo省略了很多步骤

  • 相关阅读:
    Rsync实现文件同步的算法(转载)
    Python模拟登录cnblogs
    负载均衡中四层和七层的介绍(转帖)
    Lvs+Keepalived实现MySQL高可用
    MySQL配置主主及主从备份
    Vim扩展YouCompleteMe插件
    使用Git
    Django回忆录
    Ansible安装配置及使用
    Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解
  • 原文地址:https://www.cnblogs.com/bao6/p/10360352.html
Copyright © 2020-2023  润新知