1.Struts2概要
- Struts2のフレームアプリケーションのJavaEE三層のWeb層のフレーム
- struts1やWebWorkの基礎に新たな枠組みの開発のためのStruts2の枠組み
- Struts2の間の問題を解決します:
4. structs2バージョン
5.
ウェブ層
共通フレーム:
springMVC
2.Structs2フレームエントリ
1.インポートジャーパッケージ
Mavenの方法:pom.xml設定依存:
<依存><groupIdを> JUnitの</ groupIdを><たartifactId> JUnitの</たartifactId><バージョン> 3.8.1 </バージョン><スコープ>テスト</スコープ></依存関係><依存><groupIdを> org.apache.struts </ groupIdを><たartifactId> Struts2のコア</たartifactId><バージョン> 2.5.16 </バージョン></依存関係><依存><groupIdを> org.apache.struts.xwork </ groupIdを><たartifactId> xworkのコア</たartifactId><バージョン> 2.3.34 </バージョン></依存関係><依存><groupIdを>コモンズ・ログ</ groupIdを><たartifactId>コモンズ・ログ</たartifactId><バージョン> 1.2 </バージョン></依存関係>
2.アクションを作成します。
3.設定アクションクラスのアクセス・パス
1)設定Struts2のコアプロファイル
名前: でSRC 位置: struts.xml ( 固定 )
制約DTDの2)導入
<!DOCTYPEは ストラット PUBLIC" - // Apache Software Foundationの// DTD Strutsの設定2.5 // EN""http://struts.apache.org/dtds/struts-2.5.dtd">3)構成のWeb.xml --->フィルタ
<?xmlののバージョン = "1.0" エンコード = "UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>Struct2day1</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.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- 低版本对应的class可能有细微差别 --></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
4.Struts2基本流程
5.Struts2配置
Struts2核心配置文件 -> struts.xml
结构:
<package><action><result> </result></action></package>
1) package 标签
类似于代码中的包,用于区分不同的action
三个属性:
- name : name属性值和功能本身没有关系,在一个配置文件中可以写多个package标签,name属性值是不能相同的
- extends:属性值是固定的 ‘ struts-default ’ ;写了合格属性后,在package里配置的类具有action功能
- namespace :name属性值要和action中的name属性值构成访问路径 默认值'/'
2)action标签
action标签配置action访问路径
action标签属性:
- name : name属性值要和action中的name属性值构成访问路径,package里可以有多个action,但是他们的name值不能相同
- class : action全路径: 包名+类名
- method : 比如在 action里面默认执行的方法 execute方法,但是在 action里面写其他的方法,让 action里面多个方法执行,使用 method进行配置
3)result标签
根据action返回值,配置到不同页面中去
result标签属性:
- name : 和方法返回值一样
- type : 配置如何到页面中去(转发、重定向) 默认转发
4)常量配置
6.分模块开发
单独写配置文件,把配置文件引入到核心配置文件中
<include file="per/wzy/Action/hello.xml"></include> <!--per/wzy/Action => 包名 -->
7.Action编写方式
- 第一种 : 创建普通类 ,不继承任何类、不实现任何接口
- 第二种 : 创建类 ,实现接口Action
- 第三种 : 创建类 , 继承类ActionSupport (常用)
8.访问Action的方法
访问action有三种方式
- 一:在action中添加method属性,填写要访问的方法 (缺陷:每个action都需要配置,若action里有多个方法需要执行,则需要配置大量action)
<action name="hello" class = "per.wzy.Action.Hello" method="add"><!-- 配置方法的返回值到不同的页面 --><!-- <result name ="ok">/Hello.jsp</result>--></action>
- 二:使用通配符实现,在action中的name属性中添加 * 通配符,匹配任意字符 {第几个*} 代表*替代的值
注:
s
truts2 2.5后禁用通配符,需要在package 中添加 strict-method-invocation="false"
<struts><package name="" extends="struts-default" strict-method-invocation="false"><action name="stu_*" class="per.wzy.Action.Hello" method="{1}"><allowed-methods></allowed-methods></action></package></struts>
访问http://localhost:8080/Struct2day1/stu_add和http://localhost:8080/Struct2day1/stu_del分别执行add()方法和del()方法
- 三:动态访问实现
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">