1.到官网下载 struts2.3.4
2.把lib下的
1)commons-fileupload-1.2.2
2)commons-io-2.0.1
3)commons-lang-2.4
4)commons-lang3-3.1
5)commons-logging-1.1.1
6)commons-logging-api-1.1
7)freemarker-2.3.19
8)javassist-3.11.0.GA
9)ognl-3.0.5
10)struts2-core-2.3.4.1
11)xwork-core-2.3.4.1
这11jar拷贝到项目lib下
3.配置web.xml
web.xml添加
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
4.配置struts.xml
1)struts.xml方在src下
2)struts.xml中添加如下配置
/p>
"http://struts.apache.org/dtds/struts-2.0.dtd">
/success.jsp
/failure.jsp
3)编写LoginAction类
packagecom.web;importcom.opensymphony.xwork2.ActionSupport;public class LoginAction extendsActionSupport{public String message ="Hello Word";privateString username;privateString passowrd;public String execute() throwsException {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxx");
username="1";if (username.equals("struts2")) {
System.out.println("aaaaaaaaaa");return "success";
}else{
System.out.println("dddddddddd");return "error";
}
}
public String abc(){
System.out.println("abc");
return "success";
}
public String cba(){
System.out.println("cba");
return "error";
}
}
这样 你可以可以通过 hppt://pc-xxx:8080/xxx/login.action来访问了
5、如何让struts2 实现零配置
在导入之前jar的基础上,再增加3个jar,它们分别为
1)struts2-convention-plugin-2.3.4.1
2)asm-3.3
3)asm-commons-3.3
4)web.xml在之前的配置基础上不变,改变struts.xml的配置,struts.xml配置如下:
/p>
"http://struts.apache.org/dtds/struts-2.0.dtd">
说明:
为action包
如果你action类在 com.web下,那么value="web"
如图:
为你的jsp模板放在WebRoot下的哪个位置,如果为WebRoot的根目录下,value='/',如果为WebRoot的xxx文件夹下,则value='/xxx'
4)此时 模板的命名要以action的类名来命名相对于来命名,如LoginAction,则对于的user-返回的string来命名,如:
login-success.jsp
login-error.jsp
5)此时 你就可以通过 hppt://pc-xxx:8080/xxx/login.action、
hppt://pc-xxx:8080/xxx/login!abc.action、
hppt://pc-xxx:8080/xxx/login!cba.action
等来访问了
该配置经本人测试成功,如您出现不成功的情况,请检查是否出现包冲突。