struts2框架快速创建
1.导包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
2.准备action
public class HelloAction {
public String hello(){
System.out.println("hello struts2");
return "success";
}
}
3.书写核心配置文件struts.xml
<package name="hello" namespace="/" extends="struts-default">
<action name="HelloAction" class="cn.hd.action.HelloAction" method="hello">
<result name="success">/index.jsp</result>
</action>
</package>
核心配置文件
1.package
<!--
name:包的名字 随意起 但是与其他包的名字不能重复
namespace:命名空间 访问地址的前缀
extends:继承 必须继承struts-default
abstract:抽象 等待被继承 起一个标识作用
-->
<package name="hello" namespace="/" extends="struts-default">
2.action
<!--
name:给action起个名字 没有特殊固定可以随便起,他是外部访问路径名 尽量和Action类的名字保持一致
class:类的完成整路径名
method:方法名 决定这访问类中的方法
-->
<action name="HelloAction" class="cn.hd.action.HelloAction" method="hello">
3.result
<!--
name:和访问的方法的返回值保持一致
type:转发 重定向
标签中的值 要跳转的页面
-->
<result name="success" type="redirect">/index.jsp</result>
4.include
<!--引入其他配置文件-->
<include file="struts1.xml"></include>
struts常量配置
1.常见的常量
struts.devMode = false
struts.enable.DynamicMethodInvocation = false
struts.action.extension=action,,
struts.i18n.encoding=UTF-8
2.修改常量
(1)直接在struts.xml中修改
<constant name="struts.devMode" value="true"></constant>
(2)在web.xml中修改
<context-param>
<param-name>struts.action.extension</param-name>
<param-value>do</param-value>
</context-param>
(3)创建一个struts.properties配置文件
struts2的高级配置
1.动态方法的开启
有两种
(1)
*占位符
<action name="helloAaction_*" class="cn.hd.action.HelloAction" method="{1}">
(2)开启常量
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
method不写,然后地址名和方法名之间使用!号隔开