Struts2 + Hibernate完成单表新闻管理功能

来源:互联网 发布:淘宝怎么开服装定制店 编辑:程序博客网 时间:2024/06/08 14:13

将新闻的映射文件选择加入到hibernate.cfg.xml中:


后台完成后,在成功页suc.jsp中加入两个超连接。

        <center>

            用户登陆成功,当前登陆用户为: ${user.realName}

            <br/>

            <hr/>

            <br/>

            <a href="pages/news/news_insert.jsp">添加新闻</a>

            <br />

            <a href="news!list.action">新闻列表</a>

            <br />

        </center>


先完成添加功能,编写news_insert.jsp

        <center>

            <formaction="news!insert.action"method="post">

                新闻标题:<inputtype="text"name="news.title"/> <br/>

                新闻内容:<inputtype="text"name="news.content"/> <br/>

                新闻发布日期:<inputtype="text"name="news.pubDate"/> <br/>

                <inputtype="submit"value="添加"/>

            </form>

        </center>


完成Action,实现insert方法。

public class NewsAction extends ActionSupport {

 

    private Newsnews;

 

    // 中间公共页要传递的数据

    // 提示的信息

    private Stringmessage;

    // 经过一定时间后,自动进入的页面路径

    private Stringurl;

 

    public String insert()throws Exception {

        ServiceFactory.getINewsServiceInstance().insert(news);

        message = "新闻发布成功";

        url = "pages/suc.jsp";

 

        return"forward";

    }

在struts.xml中配置forward路径

        <actionname="news"class="org.liky.action.NewsAction">

            <resultname="forward">/forward.jsp</result>

        </action>


编写forward.jsp,完成定时跳转的公共页功能。

        <scripttype="text/javascript">

            var time= 5;

            function countDown(){

                document.getElementById("time_span").innerHTML= time;

                time--;

                if(time >= 0) {

                    // js中定时调用的方法

                    window.setTimeout("countDown();",1000);

                }

            }

        </script>

    </head>

 

    <bodyonload="countDown();">

        <center>

            <%

                response.setHeader("refresh","5;URL=" + basePath + request.getAttribute("url"));

            %>

            ${message },<spanid="time_span">5</span> 秒后自动跳转!<br/>

            <ahref="${url}">如果没有跳转,请点这里!</a>

        </center>

        <br>

    </body>


实现分页列表功能,先在Action中接收参数,并查询当前页的数据以及全部记录数,传递到页面上显示。

    public String list()throws Exception {

        Map<String, Object> map = ServiceFactory.getINewsServiceInstance()

                .list(pageNo, pageSize, keyword, column);

 

        allNews = (List<News>) map.get("allNews");

        count = (Integer) map.get("allCount");

       

        return"list";

    }

    // 分页中要接收的参数

    private int pageNo = 1;

    private int pageSize = 1;

    private Stringkeyword = "";

    private Stringcolumn = "title";

 

    // 向页面上传递的列表数据

    private List<News>allNews;

    private int count;

配置跳转路径

        <actionname="news"class="org.liky.action.NewsAction">

            <resultname="forward">/forward.jsp</result>

            <result name="list">/pages/news/news_list.jsp</result>

        </action>

完成news_list.jsp

            <tableborder="1"width="80%">

                <tr>

                    <td>

                        编号

                    </td>

                    <td>

                        标题

                    </td>

                    <td>

                        内容

                    </td>

                    <td>

                        发布日期

                    </td>

                    <td>

                        操作

                    </td>

                </tr>

                <c:forEachvar="news"items="${allNews}">

                    <tr>

                        <td>

                            ${news.id }

                        </td>

                        <td>

                            ${news.title }

                        </td>

                        <td>

                            ${news.content }

                        </td>

                        <td>

                            <fmt:formatDatevalue="${news.pubDate}"pattern="yyyy-MM-dd"/>

                        </td>

                        <td>

                            修改

                            删除

                        </td>

                    </tr>

                </c:forEach>

            </table>

这里显示出了第一页的数据

 

先实现基本的翻页功能(上一页和下一页)

            <%

                int pageNo = (Integer)request.getAttribute("pageNo");

                int pageSize = (Integer)request.getAttribute("pageSize");

                int count = (Integer)request.getAttribute("count");

                // 总页数

                int allPages = (count - 1) / pageSize + 1;

            %>

            <formaction="news!list.action"method="post"id="split_page">

                <inputtype="hidden"id="pageNo"name="pageNo"value="<%=pageNo%>"/>

                <inputtype="button"value="首页"onclick="changePage(1);">

                <inputtype="button"value="上一页"onclick="changePage(<%=pageNo - 1%>);">

                <inputtype="button"value="下一页"onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value="尾页"onclick="changePage(<%=allPages%>);">

            </form>

           

            <scripttype="text/javascript">

                function changePage(newPage){

                    document.getElementById("pageNo").value= newPage;

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

                }

               

            </script>

加入限制,防止用户操作超过范围

            <formaction="news!list.action"method="post"id="split_page">

                <inputtype="hidden"id="pageNo"name="pageNo"value="<%=pageNo%>"/>

                <inputtype="button"value="首页"<%=pageNo==1?"disabled":""%> onclick="changePage(1);">

                <inputtype="button" value="上一页"<%=pageNo==1?"disabled":""%> onclick="changePage(<%=pageNo - 1%>);">

                <inputtype="button"value="下一页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value="尾页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=allPages%>);">

            </form>

 

加入跳转到某一页的功能,常见的有以下几种形式:

1)文本框形式:

            <%

                int pageNo = (Integer)request.getAttribute("pageNo");

                int pageSize = (Integer)request.getAttribute("pageSize");

                int count = (Integer)request.getAttribute("count");

                // 总页数

                int allPages = (count - 1) / pageSize + 1;

            %>

            <formaction="news!list.action"method="post"id="split_page"onsubmit="return checkPageNo();">             

                <inputtype="button"value="首页"<%=pageNo==1?"disabled":""%> onclick="changePage(1);">

                <inputtype="button"value="上一页"<%=pageNo==1?"disabled":""%> onclick="changePage(<%=pageNo - 1%>);">

                <inputtype="button"value="下一页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value="尾页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=allPages%>);">

               

                跳转到

                <inputtype="text"id="pageNo"name="pageNo"value="<%=pageNo%>"size="3"maxlength="3"/>

                页 / 共 <%=allPages%>

                <inputtype="submit"value="Go"/>

                 

            </form>

           

            <scripttype="text/javascript">

                function changePage(newPage){

                    document.getElementById("pageNo").value= newPage;

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

                }

               

                function checkPageNo(){

                    var pageNo= document.getElementById("pageNo").value;

                   

                    // js,使用  /^规则$/.test(内容)  来判断内容是否符合规则的要求.

                    // \d 表示是否为数字, +表示前面的内容必须存在一个以上,两个和到一起表示 数字要有1个以上,不能有其他内容.

                    // \w 表示字母数字或下划线

                    // ? 表示可以出现0次或1,但不能出现多次

                    // . 在正则表达式中表示任意字符  

                    // 比如:验证邮件地址的规则        \w+@\w+\.\w+  

                      

                    if(/^\d+$/.test(pageNo)){

                        if(pageNo > 0 && pageNo <= <%=allPages%>){

                            return true;

                        }

                    }

                    returnfalse;

                }

               

            </script>

2) 下拉列表形式

               

                跳转到

                <selectid="pageNo"name="pageNo"onchange="changePage(this.value);">

                    <%

                        for (int i = 1;i <= allPages;i++) {

                    %>

                    <optionvalue="<%=i%>"<%=i==pageNo?"selected":""%>><%=i%></option>

                    <%

                        }

                    %>

                </select>

                页 / 共 <%=allPages%>

               

3)数字形式

            <formaction="news!list.action"method="post"id="split_page"onsubmit="return checkPageNo();">             

                <inputtype="button"value="<<"<%=pageNo==1?"disabled":""%> onclick="changePage(1);">

                <inputtype="button"value="<"<%=pageNo==1?"disabled":""%> onclick="changePage(<%=pageNo - 1%>);">

               

                <%

                    if (pageNo - 2 > 1) {

                %>

                ...

                <%     

                    }

               

                    for (int i = pageNo - 2;i <= pageNo + 2 && i <= allPages;i++) {

                        if (i == pageNo) {

                %>

                    <fontcolor='red'><%=i%></font>

                <%         

                        } elseif (i > 0) {

                %>

                    <ahref="javascript:changePage(<%=i%>);"><%=i%></a>

                       

                <%     

                        }

                    }

               

                    if (pageNo + 2 < allPages) {

                %>

                ...

                <%     

                    }

               

                %>

               

               

                <inputtype="button"value=">"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value=">>"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=allPages%>);">

                 

                <inputtype="hidden"name="pageNo"id="pageNo"value="<%=pageNo%>"/>

            </form>

 

 

再加入改变每页显示记录数的功能

                <%

                    int[] allSize = {1,2,5,10,20};

                %>

                每页显示

                <selectid="pageSize"name="pageSize"onchange="changePage(1);">

                    <%

                        for (int i = 0 ;i < allSize.length;i++) {

                    %>

                    <optionvalue="<%=allSize[i]%>"<%=allSize[i]==pageSize?"selected":""%>><%=allSize[i]%></option>

                    <%     

                        }

                    %>

                </select>

                条数据

               

最后加入搜索功能

                <br/>

                <selectname="column"id="column">

                    <optionvalue="title">新闻标题</option>

                    <optionvalue="content" ${column=="content"?"selected":""}>新闻内容</option>

                </select>

                <inputtype="text"name="keyword"value="${keyword}"/>

                <inputtype="button"value="搜索"onclick="changePage(1);"/>

               

为了方便使用,这种分页功能一般通过动态包含的形式实现,所以将所有的分页代码,单独写到一个jsp里。

通过动态包含里的<jsp:param>来传递参数。

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags"prefix="s"%>

<%

    String path = request.getContextPath();

    String basePath = request.getScheme() + "://"

            + request.getServerName() + ":" + request.getServerPort()

            + path + "/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

        <basehref="<%=basePath%>">

 

        <title>My JSP 'index.jsp' starting page</title>

    </head>

 

    <body>

   

    <%--

        使用说明:

            将以下代码拷贝到需要显示分页内容的地方

           

            <jsp:include page="/split_page_plugin.jsp">

                <jsp:param value="${pageNo}" name="pageNo"/>

                <jsp:param value="${pageSize}" name="pageSize"/>

                <jsp:param value="${keyword}" name="keyword"/>

                <jsp:param value="${column}" name="column"/>

                <jsp:param value="${count}" name="count"/>

                <jsp:param value="news!list.action" name="URL"/>

                <jsp:param value="title:新闻标题|content:新闻内容" name="columnData"/>

                <jsp:param value="3" name="pageStyle"/>

            </jsp:include>

   

            参数说明:

                URL为查询列表的路径

                columnData是搜索选项的值,格式为:字段名1:显示文字1|字段名2:显示文字2....

                pageStyle为页面显示风格, 1是数字型, 2是文本框,其他是下拉列表

   

     --%>

   

                    <br/>

            <%

                int pageNo = Integer.parseInt(request.getParameter("pageNo"));

                int pageSize = Integer.parseInt(request.getParameter("pageSize"));

                int count = Integer.parseInt(request.getParameter("count"));

               

                String column = request.getParameter("column");

                String keyword = request.getParameter("keyword");

               

                String URL = request.getParameter("URL");

               

                // 总页数

                int allPages = (count - 1) / pageSize + 1;

            %>

            <formaction="<%=URL%>"method="post"id="split_page"onsubmit="return checkPageNo();">             

                <%

                    String pageStyle = request.getParameter("pageStyle");

                    if ("1".equals(pageStyle)) {

                %>

                <inputtype="button"value="<<"<%=pageNo==1?"disabled":""%> onclick="changePage(1);">

                <inputtype="button"value="<"<%=pageNo==1?"disabled":""%> onclick="changePage(<%=pageNo - 1%>);">

               

                <%

                    if (pageNo - 2 > 1) {

                %>

                ...

                <%     

                    }

               

                    for (int i = pageNo - 2;i <= pageNo + 2 && i <= allPages;i++) {

                        if (i == pageNo) {

                %>

                    <fontcolor='red'><%=i%></font>

                <%         

                        } elseif (i > 0) {

                %>

                    <ahref="javascript:changePage(<%=i%>);"><%=i%></a>

                       

                <%     

                        }

                    }

               

                    if (pageNo + 2 < allPages) {

                %>

                ...

                <%     

                    }

               

                %>

               

               

                <inputtype="button"value=">"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value=">>"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=allPages%>);">

                 

                <inputtype="hidden"name="pageNo"id="pageNo"value="<%=pageNo%>"/>

               

               

                <%     

                    } elseif ("2".equals(pageStyle)) {

                %>

                <inputtype="button"value="首页"<%=pageNo==1?"disabled":""%> onclick="changePage(1);">

                <inputtype="button"value="上一页"<%=pageNo==1?"disabled":""%> onclick="changePage(<%=pageNo - 1%>);">

                <inputtype="button"value="下一页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value="尾页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=allPages%>);">

               

                跳转到

                <inputtype="text"id="pageNo"name="pageNo"value="<%=pageNo%>"size="3"maxlength="3"/>

                页 / 共 <%=allPages%>

                <inputtype="submit"value="Go"/>

                <%     

                    } else {

                %>

                <inputtype="button"value="首页"<%=pageNo==1?"disabled":""%> onclick="changePage(1);">

                <inputtype="button"value="上一页"<%=pageNo==1?"disabled":""%> onclick="changePage(<%=pageNo - 1%>);">

                <inputtype="button"value="下一页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=pageNo + 1%>);">

                <inputtype="button"value="尾页"<%=pageNo==allPages?"disabled":""%> onclick="changePage(<%=allPages%>);">

                               

                跳转到

                <selectid="pageNo"name="pageNo"onchange="changePage(this.value);">

                    <%

                        for (int i = 1;i <= allPages;i++) {

                    %>

                    <optionvalue="<%=i%>"<%=i==pageNo?"selected":""%>><%=i%></option>

                    <%

                        }

                    %>

                </select>

                页 / 共 <%=allPages%>

               

               

                <%     

                    }

               

                %>

                <%

                    int[] allSize = {1,2,5,10,20};

                %>

                每页显示

                <selectid="pageSize"name="pageSize"onchange="changePage(1);">

                    <%

                        for (int i = 0 ;i < allSize.length;i++) {

                    %>

                    <optionvalue="<%=allSize[i]%>"<%=allSize[i]==pageSize?"selected":""%>><%=allSize[i]%></option>

                    <%     

                        }

                    %>

                </select>

                条数据

                <br/>

                <selectname="column"id="column">

                    <%

                        String columnData = request.getParameter("columnData");

                        String[] columnValues = columnData.split("\\|");

                        for (int i = 0;i < columnValues.length;i++) {

                            String[] values = columnValues[i].split(":");

                    %>

                    <optionvalue="<%=values[0]%>"<%=values[0].equals(column)?"selected":""%>><%=values[1]%></option>

                    <%

                        }

                    %>

                </select>

                <inputtype="text"name="keyword"value="<%=keyword%>"/>

                <inputtype="button"value="搜索"onclick="changePage(1);"/>

                 

            </form>

           

            <scripttype="text/javascript">

                function changePage(newPage){

                    document.getElementById("pageNo").value= newPage;

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

                }

               

                function checkPageNo(){

                    var pageNo= document.getElementById("pageNo").value;

                   

                    // js,使用  /^规则$/.test(内容)  来判断内容是否符合规则的要求.

                    // \d 表示是否为数字, +表示前面的内容必须存在一个以上,两个和到一起表示 数字要有1个以上,不能有其他内容.

                    // \w 表示字母数字或下划线

                    // ? 表示可以出现0次或1,但不能出现多次

                    // . 在正则表达式中表示任意字符  

                    // 比如:验证邮件地址的规则        \w+@\w+\.\w+  

                      

                    if(/^\d+$/.test(pageNo)){

                        if(pageNo > 0 && pageNo <= <%=allPages%>){

                            return true;

                        }

                    }

                    returnfalse;

                }

               

            </script>

    </body>

</html>

 

 

点超连接,完成删除操作,因此要加入一个删除的连接路径。

<ahref="news!delete.action?news.id=${news.id}"onclick="return window.confirm('确定要删除吗?');">删除</a>                     

编写Action的操作

    public String delete()throws Exception {

        ServiceFactory.getINewsServiceInstance().delete(news.getId());

        message = "新闻删除成功";

        url = "news!list.action";

       

        return"forward";

    }

修改也需要加入超连接。

<ahref="news!updatePre.action?news.id=${news.id}">修改</a>                    

完成Action中的查询操作。

    public String updatePre()throws Exception {

        news = ServiceFactory.getINewsServiceInstance().findById(

                news.getId());

 

        return"update";

    }

 

<resultname="update">/pages/news/news_update.jsp</result>     

完成修改表单

        <center>

            <formaction="news!update.action"method="post">

                新闻标题:<inputtype="text"name="news.title"value="${news.title }"/><br/>

                新闻内容:<inputtype="text"name="news.content"value="${news.content }"/><br/>

                新闻发布日期:<inputtype="text"name="news.pubDate"value="${news.pubDate }"/><br/>

                <inputtype="hidden"name="news.id"value="${news.id }"/>

                <inputtype="submit"value="修改"/>

            </form>

        </center>

Action中实现修改操作

    public String update()throws Exception {

        ServiceFactory.getINewsServiceInstance().update(news);

        message = "新闻修改成功";

        url = "news!list.action";

       

        return"forward";

    }































0 0
原创粉丝点击