1.介绍
(1)Struts2相当于是servlet,和servelt不同的是当提交到struts.xml中之后,通过aciton标签就可以直接调用想用的方法,减少了代码量。
(2)在ActionSuport中set方法是注入,即是set方法是获取jsp页面中传过来的值,get方法是是向jsp页面发送值,值得一提的是有了get和set方法之后就取代了servlet中的
request.getParameter("");和重定向的操作。在这里要重点理解get和set方法的使用。
(3)在struts2分页中set和set方法更能突出它的用法。
2.下面来看实例把!
(1)首先访问这个页面时就应该查找出nowPage为1信息,所以在action中要判断nowPage是否为空。
在jsp页面的分页判断可以写为:
<div>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=1">首页</a>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=${nowPage-1<=1?1:nowPage-1}">上一页</a>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=${nowPage+1>=page.countPage?page.countPage:nowPage+1}">下一页</a>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=${page.countPage}">末页</a>
</div>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
当然可以在Page中封装好nowPage的判断,在这里主要介绍action。
当点击下一页的时候会连接到findAllPro.action这个struts.xml中的action,
struts.xml 文件为:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="pro" namespace="/" extends="struts-default">
<action name="findAllPro" class="cn.csdn.hr.action.ProvinceAction" method="findAll">
<result>./list.jsp</result>
</action>
</package>
</struts>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
从struts.xml中可以看出findAllPro连接到的是cn.csdn.hr.action.ProvinceAction下的findAll方法,我们去找ProvinceAction,为:
// 分页查询
public String findAll() {
if ("".equals(nowPage) || nowPage == null) {
this.nowPage = 1;
}
System.out.println(nowPage + "==============");
page = new PageWhere("Province", nowPage, "where 1<2");
// page= page.getDatas();
return SUCCESS;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
因为要返回到jsp页面,所以要写一个get方法来把page传到jsp页面中:
// 得到page的值 ,并把得到的值放到page中,page可以获取所有的东西
private PageWhere page;
public PageWhere getPage() {
return page;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
这样就把当前页的信息传到了jsp中,但是在分页的时候因为nowPage是jsp和struts2来回传的值,并且是一个变量,所以设置一个属性nowPage,生成get和set方法,来获取nowPage和把修改的nowPage传到jsp中,整个ProvinceAction页面为:
package cn.csdn.hr.action;
import cn.csdn.hr.HibernateUtil.PageWhere;
import cn.csdn.hr.domain.Province;
import cn.csdn.hr.service.ProvinceService;
import cn.csdn.hr.service.ProvinceServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
public class ProvinceAction extends ActionSupport {
/**
* get方法是向jsp页面中传值 set方法向获取jsp页面的值
*/
private static final long serialVersionUID = 1L;
private ProvinceService provinceService = new ProvinceServiceImpl();
private Integer id;
private String name;
public ProvinceAction() {
super();
}
// 通过页面注入进来的。id的名称一定要和表单中 的一致
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
// 接收传过来的nowPage
private Integer nowPage;
public void setNowPage(Integer nowPage) {
this.nowPage = nowPage;
}
public Integer getNowPage() {
return nowPage;
}
// 得到page的值 ,并把得到的值放到page中,page可以获取所有的东西
private PageWhere page;
public PageWhere getPage() {
return page;
}
// 分页查询
public String findAll() {
if ("".equals(nowPage) || nowPage == null) {
this.nowPage = 1;
}
System.out.println(nowPage + "==============");
page = new PageWhere("Province", nowPage, "where 1<2");
// page= page.getDatas();
return SUCCESS;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
整个jsp页面为:
<body>
<div align="center">
<h3>
省市操作
</h3>
<table border="1px" cellpadding="0px" cellspacing="0px" width="300px">
<tr>
<th>
编号
</th>
<th>
省名
</th>
<th>
操作
</th>
</tr>
<tbody>
<c:forEach items="${page.datas}" var="entity">
<tr align="center">
<td>
${entity.id}
</td>
<td>
${entity.name}
</td>
<td>
<a href="${pageContext.request.contextPath}/insert.jsp">添加</a>
<a href="${pageContext.request.contextPath}/delPro.action?id=${entity.id}">删除</a>
<a href="${pageContext.request.contextPath}/updatePro.action?id=${entity.id}">编辑</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<br/>
<div>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=1">首页</a>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=${nowPage-1<=1?1:nowPage-1}">上一页</a>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=${nowPage+1>=page.countPage?page.countPage:nowPage+1}">下一页</a>
<a href="${pageContext.request.contextPath}/findAllPro.action?nowPage=${page.countPage}">末页</a>
</div>
</div>
</body>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
以上数据和从hibernate配置的数据库中读取出来的,分页学会了,这个就是小case了。主要看其中的注释就ok了。但是在写更新的时候需要注意:
更新需要在更新完之后要在内部跳转到查询的页面,所以结果应写为:
<result type="chain">findAllPro</result>
- 1.
记住要写type、类型。
原文链接:http://blog.csdn.net/hanxiaoshuang321123/article/details/7323116
【编辑推荐】