java action例子_Java Struts2简单入门实例和登录实例

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2 pageEncoding="ISO-8859-1"%>

3

4

5

6

7

8

Hello Struts2 Form

9

10

11

Hello Struts2 Form

12

13 Please enter your name

14

15

16

">Hello Struts Model

17

18

19

View Code

HelloStruts.jsp

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2 pageEncoding="ISO-8859-1"%>

3

4

5

6

7

8

Hello Struts2

9

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

View Code

success.jsp

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值