Structs2 HelloWorld-01 环境搭建

来源:互联网 发布:杭州软件信息技术开发 编辑:程序博客网 时间:2024/06/05 02:14

一、创建structs2工程的步骤

1、创建webproject,添加structs支持

2、加入jar包,用myeclipse添加structs支持时,会自动添加

3、配置web.xml,配置structs的核心控制器StrutsPrepareAndExecuteFilter,在添加structs支持的同时,已经添加好了

web.xml文件

<?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>Structs2-01</display-name>  <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>  <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>*.action</url-pattern>  </filter-mapping></web-app>

4、添加 DTD 约束

5、编写action类,处理业务逻辑

1.action 代表一个struct请求
2.action类 能够处理struct请求的类
3.action类的特点

  • 属性的名字必须遵守与javabeans属性名相同的命名规范
  • 必须有一个不带参的构造器
  • 至少有一个供structs在执行这个action时调用的方法
  • 一个action类可能包含多个action方法
  • struct2惠威每个http请求创建一个新的action实例,即action不是单例的,是线程安全的
6、配置structs.xml ,注册action类
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 使用packet来组织模块 --><package name="helloworld" extends="struts-default"><!-- 配置action 一个structs2的请求就是一个action name对应一个structs2的请求的名字,(或者是对应一个servlet-path,但去除/和扩展名) 不包含扩展名 result表示结果return 可能有多个结果,多个结果之间用name属性标识还有一个type属性,表示结果的类型,默认为dispatcher,转发到结果 --><action name="product-input"><!-- 配置结果 --><result>WEB-INF/pages/input.jsp</result></action><!-- 这个action请求指定class,并且调用对应类的方法方法的返回值必须和name相同 --><action name="product-save" class="com.weixuan.structs2.Product"method="save"><result name="details" type="dispatcher">WEB-INF/pages/details.jsp</result></action></package></struts>    

7、访问action
http://localhost/appName/<PackageNamespace>/<ActionName>.<Extension>
<PackageNamespace> 代表你在struts.xml中配置package的namespace,action配置中name! <Extension>默认可不写或者是.action

0 0