项目____SSH框架jsp-action映射、传参、重定向再理解

来源:互联网 发布:电脑没网络怎么回事 编辑:程序博客网 时间:2024/05/20 16:34

实现的功能:翻页查询附带查询条件,更新后留在所更新内容的位置

JSP1:

<span style="white-space:pre"></span><form id="formPage"  method="post" action="${ctx}/basicInfor/ddsManage/dds!ogc.action"> <span style="white-space:pre"></span><!-- 这里的hidden型的数据随表提交 用于翻页查询时保留查询数据 调用的action除非要在action内修改传入的数据 否则不需要在外部声<span style="white-space:pre"></span>明private型的同名变量来获取此传入值,而在mybatis的xml中写sql语句时 可以直接使用${starttime}来获取范围内key为starttime的开<span style="white-space:pre"></span>始时间 --><input type="hidden" id="starttime" name="starttime" value="${data.starttime }" /><table class="table" style="border-bottom: 1px solid #ddd; font-size: 12px;"><!-- table-striped --><thead></thead><span style="white-space:pre"></span><tobody></tbody></table><%@ include file="/common/bspage.jsp"%></form>
#若不设置method:由于默认方法是get,get是通过url提交信息,导致提交的中文字段必须编码,在接收端必须解码,否则会因为出现的编码错误导致数据错误。

#若不设置action:当操作更新打分项时,由于返回的url路径不是查询.action而是更新.action,导致翻页时请求的action也是更新.action,可能因为没有相应的参数导致空指针等内部错误。


JSP2:

<span style="white-space:pre"></span><s:form  id="uploadForm"  method="post"  theme="simple" enctype="multipart/form-data"><input type="hidden" id="starttime" name="starttime" value="${data.starttime }" /><input type="hidden" id="selPageRow" name="selPageRow" value="${data.selPageRow }" />            <table align="center" width="50%" border="1">                 <tr>                     <td>图片描述</td>                     <td >                         <s:file id="imgfile" name="imgfile"></s:file>                     </td>                 </tr>                 <tr>                     <td>区域描述</td>                     <td><s:textfield id="key_description" name="key_description"></s:textfield></td>                 </tr>                                  <tr>                     <td>评分</td>                     <td><s:select id="key_score" name="key_score" list="#{'1':'1','2':'2','3':'3','4':'4'}" <span style="white-space:pre"></span>label="评分" headerKey="3" headerValue="3"></s:select></td>                 </tr>               <tr>                                   <span style="white-space:pre"></span><td><s:reset value=" reset "></s:reset></td>                <span style="white-space:pre"></span></tr>              </table><span style="white-space:pre"></span></s:form>
通过以下操作,

<input type="hidden" id="starttime" name="starttime" value="${data.starttime }" /><input type="hidden" id="selPageRow" name="selPageRow" value="${data.selPageRow }" />

将页码信息,搜索信息等传入action中。


action:

声明——提交到action的需要被操作的数据的同名变量。

<span style="white-space:pre"></span>private String starttime;<span style="white-space:pre"></span>private String endtime;<span style="white-space:pre"></span>private String product_line2;<span style="white-space:pre"></span>private String banci2;<span style="white-space:pre"></span>private String banzu2;</span><span style="white-space:pre"></span>private int selShowpageNO;<span style="white-space:pre"></span>private int selPageRow;<span style="white-space:pre"></span>public void setSelShowpageNO(int selShowpageNO) {<span style="white-space:pre"></span>this.selShowpageNO = selShowpageNO;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public int getSelShowpageNO() {<span style="white-space:pre"></span>return selShowpageNO;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setSelPageRow(int selPageRow) {<span style="white-space:pre"></span>this.selPageRow = selPageRow;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public int getSelPageRow() {<span style="white-space:pre"></span>return selPageRow;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>..省略了..

通过以上,可以在action中操作这些变量,将其赋值到data中

<span style="white-space:pre"></span>data.set("starttime", starttime);<span style="white-space:pre"></span>data.set("endtime", endtime);<span style="white-space:pre"></span>data.set("product_line2", product_line2);<span style="white-space:pre"></span>data.set("banci2", banci2);data.set("banzu2", banzu2);data.set("selShowpageNO", selShowpageNO);data.set("selPageRow", selPageRow);

返回特定字符串

return "reload";

根据返回的字符串决定重定向位置

@Results({ @org.apache.struts2.convention.annotation.Result(name = "reload", location = "dds!ogc.action?starttime=${data.starttime}&endtime=${data.endtime}" +"&product_line2=${data.product_line2}&banci2=${data.banci2}&banzu2=${data.banzu2}", type = "redirectParams") })
#注意,这里使用的是data中的数据,data.starttime所以需要声明同名变量,setget,对data赋值。如果只是使用提交上来的${starttime}关键字,估计可以直接使用

#注意,前面设置了selxx的相关内容,这里action地址中不需要具体设置,会自动添加进去。应该是page相关项自己的方法?



0 0
原创粉丝点击