struts2入门
-
环境搭建
1.1 jar
1.2 web.xml
1.3 struts.xml
struts.xml(核心配置文件) -
开发
2.1 Action
2.1.1 不需要指定父类(ActionSupport)
2.1.2 业务方法的定义
public String xxx();//execute
2.1.3 Action是多例模式(注:在spring中的配置中一定要注意)
Action用来接收参数
2.2 参数赋值
2.2.1 Action中定义属性,并提供get/set方法
userName, getUserName/setUserName
2.2.2 ModelDriven
返回实体,不能为null,不需要提供get/set方法
2.2.3 ModelDriven返回实体和Action中属性重名,ModelDriven中优先级更高
注:ognl,ActionContext学完就知道了
2.3 与J2EE容器交互
2.3.1 非注入
2.3.1.2 耦合
ServletActionContext
2.3.1.2 解耦(建立使用解耦模式)
ActionContext
2.3.2 注入
2.3.2.1 耦合
作业:找出struts2中其它的几个注入接口(XxxAware)
2.3.2.2 解耦
–src
–struts.xml(核心配置文件)
–struts.properties(全局属性文件)
3. 核心文件配置
3.1 include 包含文件
file
3.2 package 包
name 包名
extends 继承
namespace 虚拟路径
abstract 通常用来被继承
3.3 action 子控制器
name:helloAction,helloAction_*
class 全限定名
method:execute,{1}
注1:动态方法调用,新版本中已禁用,可自行开启或关闭
注2:子控制器的访问路径:名称空间+"/"+子控制器名字_xxx+".action"
如何添加struts.xml的DTD验证
1、在项目中新建一个文件夹DTD,然后将struts2.dtd文件复制到此文件夹中
2、然后选择菜单“window ->preferences-> MyEclipse-> Files and Editors-> XML ->XML ->Catalog”
3、点击“ADD”,然后修改弹出窗口中的内容(注:Location的值是通过边上的按钮选择出来的、key Type选择下拉框中的URI即可、key的值是从struts.xml文件复制过来的)
4、4.关闭struts2.xml文件重新打开,那么在开发过程中编写配置文件时,就有相关提示了
5、记得校正validate
简单的一个helloword
先配置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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
把StrutsPrepareAndExecuteFilter类配置好
接下来配置struts的三个xml文件
struts-base.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 调试 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.i18n.reload" value="true" />
<!-- 动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 抽象包一般定义是为了被继承 -->
<package name="base" extends="struts-default" abstract="true">
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
</struts>
struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--
namespace:在内存中划分具体的控件
name:给包取个名字
-->
<package name="sy" extends="base" namespace="/sy">
<action name="helloAction" class="com.zking.one.web.HelloAction">
<result name="success">/success.jsp</result>
</action>
<action name="calAction_*" class="com.zking.one.web.CalAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="paramAction" class="com.zking.one.web.ParamAction">
<result name="rs">/rs.jsp</result>
</action>
<action name="caseAction" class="com.zking.one.web.CaseAction">
<result name="rs">/rs.jsp</result>
</action>
<action name="demo7Action" class="com.zking.test.Demo7">
<result name="success">/success.jsp</result>
</action>
<action name="tagAction" class="com.zking.three.TagAction">
<result name="demo3">/demo3.jsp</result>
</action>
</package>
</struts>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<include file="struts-sy.xml"></include>
</struts>
jsp页面
<h1>Helloword</h1>
<!-- 使用struts框架,没有配置跳转方法,那么默认调用execute方法 -->
<a href="${pageContext.request.contextPath }/sy/helloAction.action">hello</a>
写一个HelloAction
package com.zking.one.web;
public class HelloAction {
public String execute() {
System.out.println("hello,struts");
return "success";
}
}
struts-sy.xml里面配置跳的页面
就完成了