经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Struts2 » 查看文章
Struts2的常见配置 Action的访问
来源:cnblogs  作者:bao666  时间:2019/2/11 9:36:56  对本文有异议

 struts2的概要

什么是Struts2?

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

  • Spring MVC也是一个web层的框架

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

 

 struts2的入门例子

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

1 创建一个jsp界面

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

 

2 编写一个HelloAction的类

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

 

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

  1. <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC
  2.                 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  3.                 "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 配置前端过滤器

 

  1. <?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>
  2.  </filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

5 跳转成功的界面

  1. <%@ 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

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

 

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

  1.    <!--通配符的配置--><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中列表的请求路径

  1.  
  2.       客户列表

编写Action类

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

 

此demo省略了很多步骤

原文链接:http://www.cnblogs.com/bao6/p/10360352.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号