Java Struts2简单入门实例和登录实例
1 jsp出发action
2 struts2拦截请求,调用后台action
3 action返回结果,由不同的jsp展现数据
项目结构:
src
struts.xml
com
hellostruts2
action
HelloStrutsAction.java
LoginAction.java
model
HelloMessage.java
WebContent
HelloStruts.jsp
index.jsp
login
error.jsp
login.jsp
success.jsp
WEB-INF
web.xml
classes
logging.properties
mess.properties
mess_zh_CN.properties
lib
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
ognl-3.0.14.jar
struts2-core-2.3.28.1.jar
xwork-core-2.3.28.1.jar
Web.xml
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
Struts 2
index.jsp
struts2CleanupFilter
org.apache.struts2.dispatcher.ActionContextCleanUp
struts2Filter
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2CleanupFilter
/*
REQUEST
FORWARD
struts2Filter
/*
View Code
Struts.xml
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
/index.jsp
/HelloStruts.jsp
/login/login.jsp
/login/success.jsp
/login/error.jsp
View Code
logging.properties
org.apache.catalina.core.ContainerBase.[Catalina].level =INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \java.util.logging.ConsoleHandler
mess.properties
loginPage=loginPage
errorPage=errorPage
successPage=succPage
errorTip=sorry\uFF0C login failed
successTip=welcome{0},login success
user=username
pass=password
login=login
mess_zh_CN.properties
loginPage=登陆界面
errorPage=失败界面
successPage=成功界面
errorTip=对不起,您不能登录!
successTip=欢迎,{0},您已经登录!
user=用户名
pass=密 码
login=登陆
入门实例:
HelloMessage.java
packagecom.hellostruts2.model;public classHelloMessage {privateString message;publicHelloMessage(){
setMessage("Hello struts2 model.");
}publicString getMessage() {returnmessage;
}public voidsetMessage(String message) {this.message =message;
}
}
View Code
HelloStrutsAction.java
packagecom.hellostruts2.action;importcom.hellostruts2.model.HelloMessage;public classHelloStrutsAction {privateString name;privateHelloMessage helloMessage;public String execute() throwsException{
helloMessage=newHelloMessage();
helloMessage.setMessage("Hello struts model.");return "success";
}publicString getName(){returnname;
}public voidsetName(String name){this.name=name;
}publicHelloMessage getMessage(){returnhelloMessage;
}public voidsetMessage(HelloMessage helloMessage){this.helloMessage=helloMessage;
}
}
View Code
index.jsp
1
2 pageEncoding="ISO-8859-1"%>
3
4
5
6
7
8
Hello Struts2 Form9
10
11
Hello Struts2 Form
12
13 Please enter your name
14
15
16
">Hello Struts Model
17
18
19
View Code
HelloStruts.jsp
1
2 pageEncoding="ISO-8859-1"%>
3
4
5
6
7
8
Hello Struts29
10
11
12 Hello Struts2,
13
14 Hello Model,
15 ${message.message}16
17
View Code
登录实例:
当login.jsp触发action时,就会向后抬发送login.action的请求,这个请求被后台拦截,交给struts.xml中配置的action处理。
LoginAction.java
packagecom.hellostruts2.action;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;public class LoginAction extendsActionSupport {private static final long serialVersionUID = 1L;privateString username;privateString password;publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}public String execute() throwsException{if(getUsername().equals("test") && getPassword().equals("test")){
ActionContext.getContext().getSession().put("user", getUsername());returnSUCCESS;
}else{returnERROR;
}
}public voidvalidate(){if(username ==null || username.trim().equals("")){
addFieldError("username", "user name is required");
}if(password.length()<3){
addFieldError("password", "password must be more than 3");
}
}
}
View Code
login.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
View Code
success.jsp
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
9
10
11
12
13
14
15
View Code
error.jsp
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
9
10
11
12
13
library下载:http://struts.apache.org/download.cgi