Struts2开发总结.docx

上传人:b****8 文档编号:9704504 上传时间:2023-02-05 格式:DOCX 页数:25 大小:26.03KB
下载 相关 举报
Struts2开发总结.docx_第1页
第1页 / 共25页
Struts2开发总结.docx_第2页
第2页 / 共25页
Struts2开发总结.docx_第3页
第3页 / 共25页
Struts2开发总结.docx_第4页
第4页 / 共25页
Struts2开发总结.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

Struts2开发总结.docx

《Struts2开发总结.docx》由会员分享,可在线阅读,更多相关《Struts2开发总结.docx(25页珍藏版)》请在冰豆网上搜索。

Struts2开发总结.docx

Struts2开发总结

Struts2项目开发总结

(注:

Struts2版本:

Struts2.1.6,数据库:

Oracle9i)

所须架包:

commons-logging-1.0.4.jar、commons-fileupload-1.2.1.jar、freemarker-2.3.13.jar

ognl-2.6.11.jar、struts2-core-2.1.6.jar、xwork-2.1.2.jar

1、若在前台页面使用orgl表达式:

必须定义:

<%@pageisELIgnored="false"%>

使用:

${requestScope.count}来获取session中的值

2、在jsp页面中显示action中的数据,不需要点击链接到action再返回页面中显示数据

第一步,写一个action类

第二步:

配置struts.xml文件,如:

/sort1.jsp

/error.jsp

第三步:

在sort1.jsp中遍历action方法为sort5list的结果集:

iteratorvalue="sort5list"id="ctype"status="status">

ct_id=

propertyvalue="ct_id"/>">

propertyvalue="ct_name"/>

iterator>

第四步:

在index.jsp中调用action为sort5list的方法

actionname="sort5"

executeResult="true">

iteratorvalue="sort5list"

id="ctype">

iterator>

action>

3、Struts2超链接传参方式:

第一种:

nid=

propertyvalue="nid"/>">

propertyvalue="ntitle"/>

第二种:

urlid="Newsquerynew"action="querynew.action">

paramname="request_locale">Newsquerynew

param>

url>

ahref="%{Newsquerynew}">最新文章

a>

第三种:

ahref="newsAction.action">直接使用即可

a>

注意:

Struts2超链接标签

ahref=””>中的href属性是必选参数,否则在项目整合中可能会出现不明的标签异常。

4、使用Struts2标签

iterator>循环遍历结果集:

iteratorvalue="sort5newslist">

nid=

propertyvalue="nid"/>">

propertyvalue="ntitle"/>

propertyvalue="ntime"/>

iterator>

其中内嵌html语言,

iterator>除了value属性外,还有可选属性id=""status="status",value属性是必选的

5、格式化日期显示方法

或者

,只需要改变

即可

必须定义:

<%@pageimport="java.text.SimpleDateFormat;"%>

Body体中引用:

<%SimpleDateFormats=newSimpleDateFormat("yyyy-MM-dd");

Datentime=newDate();

%><%=s.format(ntime)%>

6、Struts2下拉菜单的使用

selectname="ct_sid"list="ctypesonlist"listKey="ct_id"

listValue="ct_name"theme="simple"headerKey="0"

headerValue="一级栏目">

select>

selectname="ninvalidate"list="#{1:

'是',2:

'否'}"

listKey="key"listValue="value"headerKey="0">

select>

属性注解:

name是给Struts2

select>标签取名字,这个也是提交到action并保存到对应的数据库中的字段名所必须得参数;list是下拉选项数值;listKey是选项列表所对应的id,listValue是选项列表每个字段的名称;theme是自定义下拉框格式,headerkey是首选项的主键id,headerValue是首选项的字段名

7、Struts2.1.6使用日期控件:

需要导入包:

struts2-dojo-plugin-2.1.6.jar

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

在head体中加入:

head/>

在body体中引用:

datetimepickername="ninvalidate_time"type="date"

displayFormat="yyyy/MM/dd"value="%{'today'}">

datetimepicker>

属性注解:

name是给

detetimepicker>标签取名,type为date日期类型,displayFormat格式化显示日期,value是获取时间值,value="%{'today'}"是获取当前系统时间

8、Struts2中form提交表单格式

formaction="login"method="post"theme="simple"validate="true">

form>

在form中加入theme="simple"属性,就不会受Struts2

form>定义格式的影响

9、使用属性驱动取值

在action中将要输出的属性生成get和set方法,并将返回值定义为返回对象,如:

privateStringntitle;//标题

privateStringncontent;//内容

privateStringntime;//时间

privateStringusername;//发布人

Newsnews=ns.queryNnewsdetailsql2(nid);//返回一个对象

publicStringgetNtitle(){

returnntitle;

}

publicvoidsetNtitle(Stringntitle){

this.ntitle=ntitle;

}

publicStringgetNcontent(){

returnncontent;

}

publicvoidsetNcontent(Stringncontent){

this.ncontent=ncontent;

}

publicStringgetNtime(){

returnntime;

}

publicvoidsetNtime(Stringntime){

this.ntime=ntime;

}

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

this.username=username;

}

由action直接跳转到要显示这些属性详细内容的页面

在jsp中直接使用标签

propertyvalue="ncontent"/>取出即可

使用模型驱动:

查改

新建action查询文章类型:

importjavax.servlet.http.HttpServletRequest;

importorg.apache.struts2.ServletActionContext;

importcom.opensymphony.xwork2.ActionContext;

importcom.opensymphony.xwork2.ActionSupport;

importcom.opensymphony.xwork2.ModelDriven;

importcom.txxw.cms.news.model.Ctype;

importcom.txxw.cms.news.model.CtypeService;

/**

*@authorfejdE-mail:

lzd_niit_java@

*@version创建时间:

May22,20091:

43:

21PM

*功能实现:

文章类型修改内容查询

*/

publicclassCtypeeditActionextendsActionSupportimplementsModelDriven{

privateCtypectype=newCtype();//将封装数据的ctype实现get和set方法

@Override

publicStringexecute()throwsException{

//TODOAuto-generatedmethodstub]

HttpServletRequestrequest=ServletActionContext.getRequest();

intct_id=Integer.parseInt(request.getParameter("ct_id").replace(",",""));//通过选择复选框来实现单个文章类型修改

CtypeServicecs=newCtypeService();

Ctypectype=cs.editctypesql(ct_id);

this.setCtype(ctype);

return"success";

}

publicObjectgetModel(){

//TODOAuto-generatedmethodstub

returnnull;

}

publicCtypegetCtype(){

returnctype;

}

publicvoidsetCtype(Ctypectype){

this.ctype=ctype;

}

}

Editctypesql()方法如下:

publicCtypeeditctypesql(intct_id)throwsIOException{

dbcon=newDBConnection();

con=dbcon.getCon();

Ctypecty=null;

Listctylist=newArrayList();

try{

psta=con.prepareStatement(this.getEditCtypesql(ct_id));

psta.setInt(1,ct_id);

rs=psta.executeQuery();

while(rs.next())

{

cty=newCtype();

cty.setCt_id(rs.getInt("ct_id"));

cty.setCt_name(rs.getString("ct_name"));

cty.setCt_sid(rs.getString("ct_sid"));

//数据类型Clob转换String类型START//

try

{

oracle.sql.CLOBclob=(oracle.sql.CLOB)rs.getClob("ct_introduction");//数据库中存文本的CLOB型字段名

Stringct_introduction=clob.getSubString((long)1,(int)clob.length());//subString是截取字符串(从1截到length)

if(clob==null||ct_introduction==null||ct_introduction==""){

returnnull;

}

cty.setCt_introduction(ct_introduction);

System.out.println(ct_introduction);

}//try

catch(Exceptione){

logger.debug("数据类型Clob转换String类型出现异常");

logger.info("数据类型Clob转换String类型出现异常");

e.printStackTrace();

}

//数据类型Clob转换String类型END//

cty.setCt_image(rs.getString("ct_image"));

ctylist.add(cty);

}

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

returncty;

}

publicStringgetEditCtypesql(intct_id){

this.editCtypesql="select*fromsystem.ctypewherect_id=?

";

returneditCtypesql;

}

publicvoidsetEditCtypesql(StringeditCtypesql){

this.editCtypesql=editCtypesql;

}

注明:

上述方法中涉及到Oracle数据类型Clob转换成String类型问题

在jsp中使用

propertyvalue="ctype.ct_id"/>

即可取出ctype中的数据。

10、Struts2+ajax实现批量删除,批量更新操作

全选:

checkAll()

不选:

checkAllNo()

反选:

swichAll()

批量删除:

deletenews()

批量更新:

display1news()

functioncheckAll(){

varobj=document.getElementsByName("nid");

for(i=0;i

obj[i].checked=true;

}

}

functioncheckAllNo(){

varobj=document.getElementsByName("nid");

for(i=0;i

obj[i].checked=false;

}

}

functionswitchAll(){

varobj=document.getElementsByName("nid");

for(i=0;i

obj[i].checked=!

obj[i].checked;

}

}

functionvals(input1,input2)

{

vartemp="";

varobjForm=document.forms[input1];

varobjLen=objForm.length;

for(variCount=0;iCount

{

if(objForm.elements[iCount].type=="checkbox")

{

if(objForm.elements[iCount].checked==true&&objForm.elements[iCount].nid!

="allSelected"&&objForm.elements[iCount].nid!

="allConcled")

{

temp=temp+objForm.elements[iCount].value+",";

}

//objForm.elements[iCount].checked=true;

}

}

returntemp;

}

functiondeletenews(){

varnid=vals('form1','this')

if(nid==""){

alert('你没有选中内容');

returnfalse;

}else{

if(confirm('你确定要删除选中的内容吗'+'?

'+nid)){

varurl="delete.action?

nid="+nid;

window.location.href=url;

}

}

}

functiondisplay1news(){

varnid=vals('form1','this')

if(nid==""){

alert('你没有选中内容');

returnfalse;

}else{

if(confirm('你确定要显示选中的内容吗'+'?

'+nid)){

varurl="display1news.action?

nid="+nid;

window.location.href=url;

}

}

}

11、控制Struts2

form>提交表单样式

--form提交表单样式设置Start-->

--

form{margin:

0px;display:

inline;}

-->

--控制form表单样式-->

functionsub()

{

document.form.submit();

}

--form提交表单样式设置End-->

12、ajax前台验证(一部分)

functioncheck()

{

if(document.getElementById("ct_name").value==""){

alert("请输入栏目名称!

");

returnfalse;

}

document.form.submit();

}

functioncheck()

{

if(document.getElementById("ntitle").value==""){

alert("请输入文章标题!

");

returnfalse;

}

if(document.getElementById("nprovider").value==""){

alert("请输入供稿人!

");

returnfalse;

}

if(document.getElementById("ntelephone").value==""){

alert("请输入联系电话!

");

returnfalse;

}

varpartten1=/^1[3,5]\d{9}$/;

varpartten2=/^0(([1,2]\d)|([3-9]\d{2}))\d{8}$/;

if(partten1.test(document.getElementById("ntelephone").value)==false&&partten2.test(document.getElementById("ntelephone").value)==false)

{

alert("请检查您输入的联系电话格式");

returnfalse;

}

if(document.getElementById("email").value==""){

alert("请输入Email!

");

returnfalse;

}

varzz=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

if(zz.test(document.getElementById("email").value)==false)

{

alert("请输入正确格式的Email.");

returnfalse;

}

document.form.submit();

}

formaction="updatectype"name="form"method="post"theme="simple">

textfieldname="ctype.ct_name"id="ct_name"size="30">

textfield>

form>

 

functioncheck()

{

if(document.getElementById("nt").value=="")

{

alert("请输入查询关键字");

returnfalse;

}

document.getElementById("form2").submit();

}

按标题:

hiddenname=

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

当前位置:首页 > 求职职场 > 简历

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

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