Struct 2.1.8 环境搭建
1、找到所需的jar包:发行包的lib目录中(不同版本需要的最小jar包是不同的,参见不同版本的文档。2.1.8)和2.3.7不同,具体查询user guide
struts2-core.jar 核心jar包commons-io.jar 文件上传依赖的包
2、在应用的WEB-INF/classes目录下建立一个名称为struts.xml的配置文件,内容如下:
1、编写struts.xml配置文件
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
4、如果TOmcat启动成功,没有报错,证明环境搭建成功!
尤为注意如果修改structs2内的配置文件后要重启
(当然,如果为了方便 可以直接宝贝app下的war包中的)blank模块
Struct 2 快速入门
1、编写struts.xml配置文件
/1.jsp
package cn.itcast.action;
public class HelloWorldAction implements Serializable {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String sayHello(){
message = "helloworld by struts2";
return "success";
}
}
4、访问helloworld动作的方式:http://localhost:8080/struts2day01/test/helloworld 应用名称/包的名称空间/动作的名称
默认情况下:访问动作名helloworld,可以直接helloworld,或者helloworld.action
如果使用helloworld.do
需要增加扩展名 搜索default.properties -> action 即可
http://localhost:8080/struts2day01/test/a/b/c/helloworld
/test/a/b/c:名称空间
helloworld:动作名称
搜索顺序:名称空间
/test/a/b/c 没有helloworld
/test/a/b 没有helloworld
/test/a 没有helloworld
/test 有了,调用执行
Struts2配置文件的详解
1、struts.xml配置文件编写是没有提示的问题?
方法一:上网即可
方法二:
1、拷贝http://struts.apache.org/dtds/struts-2.1.7.dtd地址
2、Eclipse的window、preferences,搜索XML Catelog
3、点击add按钮
Location:dtd文件的路径
Key Type:URI
Key:http://struts.apache.org/dtds/struts-2.1.7.dtd
2、Struts配置文件中的各种默认值。
action:
class:默认值是com.opensymphony.xwork2.ActionSupport
常量: SUCCESS success
NONE none
ERROR error
INPUT input
LOGIN login
method:默认值是public String execute(){}
实际开发中:自己编写的动作类一般情况下继承com.opensymphony.xwork2.ActionSupport
result:
type:转到目的地的方式。默认值是转发,名称是dispatcher
(注:type的取值是定义好的,不是瞎写的。在struts-default.xml中的package中有定义)
dispatcher:普通的转发到某个页面
chain:普通的转发到某个动作名称
redirect:重定向到一个页面
redirectAction:重定向到一个动作名称
plainText:以纯文本的形式输出JSP内容
result元素的写法:
方式一:
方式二:
a2
注意:如果要转向的是在另外一个名称空间的动作,那么只能使用方式二
/3.jsp
/namespace1
a2
利用strutx.xml中的constant元素来覆盖掉default.properties默认行为