首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

[转]Struts2分面实现示范

2013-12-10 
[转]Struts2分面实现示例摘要: http://java.chinaitlab.com/List_213_11.html???? 1.介绍??????? (1)Strut

[转]Struts2分面实现示例

摘要: http://java.chinaitlab.com/List_213_11.html

?

??? 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页面的分页判断可以写为:
????
??? <p>
????
??? <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>
????
??? </p>
????
??? 当然可以在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"
????
??? struts.apache.org/dtds/struts-2.3.dtd">
????
??? <struts>
????
??? <package name="pro" namespace="/" extends="struts-default">
????
??? <action name="findAllPro" method="findAll">
????
??? <result>./list.jsp</result>
????
??? </action>
????
??? </package>
????
??? </struts>
????
??? 从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;
????
??? }
????
??? 因为要返回到jsp页面,所以要写一个get方法来把page传到jsp页面中:
????
??? // 得到page的值 ,并把得到的值放到page中,page可以获取所有的东西
????
??? private PageWhere page;
????
??? public PageWhere getPage() {
????
??? return page;
????
??? }
????
??? 这样就把当前页的信息传到了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;
????
??? }
????
??? }
????
??? 整个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}">

?

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;
????
??? }
????
??? }
????
??? 整个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}">

?

?

热点排行