基于myeclipse8的struts2x开发.docx

上传人:b****6 文档编号:6679658 上传时间:2023-01-08 格式:DOCX 页数:14 大小:218.17KB
下载 相关 举报
基于myeclipse8的struts2x开发.docx_第1页
第1页 / 共14页
基于myeclipse8的struts2x开发.docx_第2页
第2页 / 共14页
基于myeclipse8的struts2x开发.docx_第3页
第3页 / 共14页
基于myeclipse8的struts2x开发.docx_第4页
第4页 / 共14页
基于myeclipse8的struts2x开发.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

基于myeclipse8的struts2x开发.docx

《基于myeclipse8的struts2x开发.docx》由会员分享,可在线阅读,更多相关《基于myeclipse8的struts2x开发.docx(14页珍藏版)》请在冰豆网上搜索。

基于myeclipse8的struts2x开发.docx

基于myeclipse8的struts2x开发

基于myeclipse8的struts2.x开发

开发环境:

MyEclipse8.6

Struts2版本:

2.1.8.1

一、创建工程

(1)创建一个webproject:

MyStruts2

(2)为工程添加Struts2框架支持,在工程名MyStruts2上右击,选择“MyEclipse/AddStrutsCapabilities...”

(3)在弹出的对话框中选择,Struts2.1

(4)点击完成后,可以看到项目目录中多了Struts2CoreLibraries文件夹,同时src自动生成了struts.xml文件

工程中生成了一个空的struts.xml文件:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http:

//struts.apache.org/dtds/struts-2.1.dtd">

另外,Web-INF下的web.xml中多了以下几行(加入Struts2MVC框架启动配置):

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

*.action  

<--注:

应该将*.action改为/*,所有请求都可以被拦截,进而寻找对应的action去执行-->

另外:

如果是2.1.3之前的版本,用org.apache.struts2.dispatcher.FilterDispatcher;之后则用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。

从Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。

此时,运行tomcat服务器,在浏览器中输入:

http:

//localhost:

8080/MyStruts2/,应该能够正确显示页面,表明添加Struts2后,没有引起冲突。

总结:

在MyEclipse8中对一个项目中添加struts2支持方法非常简单,只需要进行简单设置,不用手动创建配置文件。

二、struts2简单用例1

示例内容:

添加一个action,在其中处理数据,并将结果在页面上进行显示。

1、添加需要的JSP页面

添加一个页面,用于跳转到的位置,如result.jsp,将页面放在WEB-INF/pages/目录下,是为了保证安全,不让用户直接输入网址进入。

2、添加Action

在struts.xml源文件中右击鼠标,选择MyEclipes/NewAction...

在新弹出的“NewAction”对话框里新建一个Package,如下图所示:

说明:

struts2的Action必须放在指定的包空间下定义,即要设置Package的name;而Namespace是一个路径,与Action名称一起组成action的路径,例如在JSP表单的action属性中设置该action路径,则要用namespace+/+actionname.action组成。

继续添加Action的Name,Class以及Results。

其中Name为action的名称,类似于struts1中的path,即程序中访问该action使用的映射名称;Class为action的完整类名,包括包名与类名。

如下图所示:

注意:

添加Results时,类型选default默认,不能选redirect,否则action将不对给页面传参数。

当然,如果需要传参,也可以通过在路径后面添加参数的方法把参数带上,如location=/WEB-INF/pages/result.jsp?

name=${username}。

实际上,这一步修改了struts.xml文件,即帮我们添加了如下代码:

extends="struts-default">

/WEB-INF/pages/result.jsp

其中,result类似于Struts1.x中的forward元素。

注意jsp页面的路径,如果在工程根目录下,就写成:

/result.jsp

这一步在工程中添加了com.gzzzz.OutputAction.java文件。

3、添加代码逻辑操作,进入OutputAction类:

publicclassOutputActionextendsActionSupport{

privateStringMsg;    //添加一个变量

publicStringgetMsg(){    //添加getter

returnMsg;

}

publicStringexecute(){

Msg="results:

"+(10+20);     //逻辑运算

returnSUCCESS;//预定义常量

}

}

2.4在result页面对msg进行显示

${msg}    //EL表达式

 

说明:

EL表达式的变量名,要与Action中的getMsg函数名要对应(即将Msg变为msg),而与相关成员变量名不必保持对应,虽然我们通常这样做。

 4、运行程序

在浏览器出输入:

http:

//localhost:

8080/MyStruts2/output.action

这样会出错。

在调用action之前要加上Namespace。

输入改为:

http:

//localhost:

8080/MyStruts2/mypath/output.action

如下,所示,看到了我们想要的结果:

 

三、struts2简单用例2

示例内容:

用户登陆功能。

1、添加需要的JSP页面

添加login.jsp,代码如下:

<%@pagelanguage="java"pageEncoding="GBK"%>

<%@tagliburi="/struts-tags"prefix="s"%>

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

LoginPage

--

-->

actionerror/>

formaction="/mystruts2/login.action"method="post">

textfieldname="username"label="username"/>

fielderrorkey="msg.error"/>

passwordname="password"label="password"/>

submitvalue="提交"/>

form>

 

2、添加Action:

这里必须重写execute方法,该方法也是Action的主要逻辑处理方法,这个方法必须返回一个String类型的值,这个字符串和struts.xml中该Action标签中某个标签的name属性值相同,也就是通过这个值确定页面定向位置。

实例代码如下:

packagecom.coffe;

importcom.opensymphony.xwork2.ActionSupport;

publicclassLoginActionextendsActionSupport{

privateStringusername;

privateStringpassword;

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

this.username=username;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

publicStringexecute(){

if(username.equals("coffe")&&password.equals("123"))

returnSUCCESS;

else

returnERROR;

}

}

3、web.xml文件修改

在MyEclipse自动生成的web.xml基础上添加如下过滤器,而且必须添加,否则运行报错。

xmlversion="1.0"encoding="UTF-8"?

>

xmlns="

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

login.jsp

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

 

4、Struts.xml文件修改

在MyEclipse自动生成的struts.xml基础上添加Action的说明,每个Action都必须包含在一个Package标签内,当然可以有多个

实例如下:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http:

//struts.apache.org/dtds/struts-2.1.dtd">

extends="struts-default">

/index.jsp

/login.jsp

/error.jsp

5、添加成功页面index.jsp :

<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+":

//"+request.getServerName()+":

"+request.getServerPort()+path+"/";

%>

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

">

MyJSP'index.jsp'startingpage

--

-->

Welcome!


6、添加失败页面error.jsp:

<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+":

//"+request.getServerName()+":

"+request.getServerPort()+path+"/";

%>

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

">

Error

--

-->

Error!


7、测试程序。

四、struts2简单用例3

示例内容:

在struts2中加入parameter。

1、Struts.xml文件如下:

(在action中加入paramName1参数)

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http:

//struts.apache.org/dtds/struts-2.1.dtd">

extends="struts-default">

1

/index.jsp

2、action中访问paramName1的方法。

Struts2使用注入法将paramName1的初始值注入到action对象中,action定义paramName1内部成员作为依赖对象,并提供getter和setter方法。

如下:

packagecom.coffe;

importcom.opensymphony.xwork2.ActionSupport;

publicclassmyAction1extendsActionSupport{

privateStringparamName1;

publicStringgetParamName1(){

returnparamName1;

}

publicvoidsetParamName1(StringparamName1){

this.paramName1=paramName1;

}

publicStringexecute(){

paramName1="paramName1:

"+paramName1;//逻辑运算

returnSUCCESS;//预定义常量

}

}

3、jsp中访问paramName1:

${paramName1}

4、访问程序:

http:

//localhost:

8080/struts2test/mypath/xxx.action

五、struts2简单用例4

示例内容:

在struts2中加入两个action,action之间进行跳转。

在程序中存在myAction1和myAction2,myAction1即是前例的action。

要求访问myAction2,然后通过myAction2跳转到myAction1。

1、Struts.xml文件如下:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http:

//struts.apache.org/dtds/struts-2.1.dtd">

extends="struts-default">

xxx

1

/index.jsp

2、myAction2的代码:

packagecom.coffe;

importcom.opensymphony.xwork2.ActionSupport;

publicclassmyAction2extendsActionSupport{

publicStringexecute(){

return"gotoXXX";

}

}

3、访问程序:

http:

//localhost:

8080/struts2test/mypath/sss.action

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1