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省略了很多步骤
下一篇: 缺铁性贫血患者怎样吃东西 两款药膳推荐你