struts2实例.docx
- 文档编号:5492183
- 上传时间:2022-12-17
- 格式:DOCX
- 页数:13
- 大小:75.92KB
struts2实例.docx
《struts2实例.docx》由会员分享,可在线阅读,更多相关《struts2实例.docx(13页珍藏版)》请在冰豆网上搜索。
struts2实例
Struts2.06+tiles
介绍:
本文档适用于新手,刚使用struts2,并且想知道怎么使用tiles的人使用,下面是我自己建立的工程,所有文件都很详尽,只要按照步奏来,就可以在自己的机器上,实现tiles的基本功能,和使用方法。
1.建立环境
集成环境是eclipes,需要的jar包,服务器用tomcat,具体目录我将在,在下面的截图给出。
2.建立一个动态的web,然后开始如图建立文件,并在apache的网站下载jar包,添加在lib下就可以了。
3.实现之后的界面如下所示:
用户名和密码都是admin
登陆之后的
4.在src下,建立包net.viralpatel.struts2,然后在包下建立三个文件,依次如下所示:
CustomerAction.java
packagenet.viralpatel.struts2;
importcom.opensymphony.xwork2.ActionSupport;
publicclassCustomerActionextendsActionSupport{
privateStringname;
privateIntegerage;
privateStringemail;
privateStringtelephone;
publicStringaddCustomer(){
returnSUCCESS;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicIntegergetAge(){
returnage;
}
publicvoidsetAge(Integerage){
this.age=age;
}
publicStringgetEmail(){
returnemail;
}
publicvoidsetEmail(Stringemail){
this.email=email;
}
publicStringgetTelephone(){
returntelephone;
}
publicvoidsetTelephone(Stringtelephone){
this.telephone=telephone;
}
}
LoginAction.java
packagenet.viralpatel.struts2;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginActionextendsActionSupport{
privateStringusername;
privateStringpassword;
publicStringexecute(){
if(this.username.equals("admin")
&&this.password.equals("admin")){
return"success";
}else{
addActionError(getText("error.login"));
return"error";
}
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
CustomerAction-validation.xml
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEvalidatorsPUBLIC
"-//OpenSymphonyGroup//XWorkValidator1.0.2//EN"
"
5,在src下建立文件ApplicationResources.properties接着建立俩个文件,依次为下面俩个文件:
ApplicationResources.properties
label.username=Username
label.password=Password
label.login=Login
error.login=Invalidusername/password.Pleasetryagain.
name=Name
age=Age
email=Email
telephone=Telephone
label.add.customer=AddCustomer
errors.invalid=${getText(fieldName)}isinvalid.
errors.required=${getText(fieldName)}isrequired.
errors.number=${getText(fieldName)}mustbeanumber.
errors.range=${getText(fieldName)}isnotintherange${min}and${max}.
struts.xml
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
value="false"/> value="ApplicationResources"/> class="org.apache.struts2.views.tiles.TilesResult"/> class="net.viralpatel.struts2.LoginAction"> class="net.viralpatel.struts2.CustomerAction"> 6.WEB-INF文件下建立文件: WEB.XML的内容为: xmlversion="1.0"encoding="UTF-8"? > xmlns=" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" org.apache.struts2.tiles.StrutsTilesListener org.apache.struts2.dispatcher.FilterDispatcher Tiles.xml的内容为: xmlversion="1.0"encoding="UTF-8"? > DOCTYPEtiles-definitionsPUBLIC "-//ApacheSoftwareFoundation//DTDTilesConfiguration2.0//EN" "http: //tiles.apache.org/dtds/tiles-config_2_0.dtd"> 7.建立所需的JSP BaseLayout.jsp <%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@tagliburi="http: //tiles.apache.org/tags-tiles"prefix="tiles"%> DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN" "http: //www.w3.org/TR/html4/loose.dtd"> insertAttributename="title"ignore="true"/> insertAttributename="header"/> insertAttributename="menu"/> insertAttributename="body"/> insertAttributename="footer"/>
Customer.jsp
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@pagecontentType="text/html;charset=UTF-8"%>
<%@taglibprefix="s"uri="/struts-tags"%>
formaction="customer.action"method="post"validate="true"> textfieldname="name"key="name"size="20"/> textfieldname="age"key="age"size="20"/> textfieldname="email"key="email"size="20"/> textfieldname="telephone"key="telephone"size="20"/> submitmethod="addCustomer"key="label.add.customer"align="center"/> form> Footer.jsp <%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@pagecontentType="text/html;charset=UTF-8"%> <%@taglibprefix="s"uri="/struts-tags"%> Copyright©ViralP Header.jsp <%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@pagecontentType="text/html;charset=UTF-8"%> <%@taglibprefix="s"uri="/struts-tags"%> Login.jsp <%@pagecontentType="text/html;charset=UTF-8"%> <%@taglibprefix="s"uri="/struts-tags"%> <%@tagliburi="prefix="c"%>Struts2Example-ViralP
Struts2-Login
actionerror/> formaction="Login.action"method="post"> textfieldname="username"key="label.username"size="20"/> passwordname="password"key="label.password"size="20"/> submitmethod="execute"key="label.login"align="center"/> form> Menu.jsp <%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@pagecontentType="text/html;charset=UTF-8"%> <%@taglibprefix="s"uri="/struts-tags"%> ahref="customer-form">Customer a> SuccessCustomer.jsp <%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><%@pagecontentType="text/html;charset=UTF-8"%> <%@taglibprefix="s"uri="/struts-tags"%>CustomerAddedSuccessfully.
Welcome.jsp
<%@pagelanguage="java"contentType="text/html;c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- struts2 实例
![提示](https://static.bdocx.com/images/bang_tan.gif)