mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、存储新闻dao方法

在NewsDao.java中新增存储新闻的saveOrupdate方法

public void  saveOrupdate(News news){
         getSession().saveOrUpdate(news);
}

二、存储新闻service方法

在NewsService.java中新增存储新闻的saveOrupdate方法

public void saveOrupdate(News news){
        newsDao.saveOrupdate(news);
}

三、存储新闻action

让NewsAction实现ModelDriven和Preparable接口,并实现其抽象方法。

package cn.ac.ucas.action;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.ac.ucas.form.News;
import cn.ac.ucas.service.NewsService;

public class NewsAction extends ActionSupport implements RequestAware, ModelDriven<News>, Preparable {

    private static final long serialVersionUID = 1L;
    private Map<String, Object> request;

    private NewsService newsService;
    private News model;

    @Override
    public void setRequest(Map<String, Object> arg0) {
        // TODO Auto-generated method stub
        this.request = arg0;
    }

    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    public String list() { // 新闻列表,获取所有新闻

        request.put("newslist", newsService.getAllNews());

        return "list";
    }

    public String save() { // 存储新闻
        // System.out.println(model);
        newsService.saveOrupdate(model);
        return "save";
    }

    public String post() { // 跳转到公布新闻页面
        return "post";
    }

    @Override
    public News getModel() {
        // TODO Auto-generated method stub
        return model;
    }

    @Override
    public void prepare() throws Exception {
        // TODO Auto-generated method stub

    }

    public void prepareSave() {
        model = new News();
    }

}

四、配置struts

改动struts.xml,使用新的拦截器栈

<package name="default" namespace="/" extends="struts-default">

        <!-- 定义新的拦截器,配置prepare拦截器栈的alwaysInvokePrepare參数值为false -->
        <interceptors>
            <interceptor-stack name="sshStack">
                <interceptor-ref name="paramsPrepareParamsStack">
                    <param name="prepare.alwaysInvokePrepare">false</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <!-- 使用新的拦截器栈 -->
        <default-interceptor-ref name="sshStack"></default-interceptor-ref>


        <action name="news-*" class="newsAction" method="{1}">
            <result name="list">/WEB-INF/newslist.jsp</result>
            <result name="post">/WEB-INF/newspost.jsp</result>
            <result name="save" type="redirect">/news-list</result>
        </action>
</package>

訪问news-post跳到/WEB-INF/newspost.jsp公布新闻。訪问news-save存储新闻到数据库并重定向到新闻列表页。

五、编写前端页面

在index.jsp增加一行,点击中后跳转至/WEB-INF/newspost.jsp

<a href="news-post">公布新闻</a>

newspost.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <s:form action="news-save" method="post">
        <s:textfield name="title" label=" 新闻标题"></s:textfield>
        <s:textfield name="author" label="新闻作者"></s:textfield>
        <s:textfield name="source" label="新闻来源"></s:textfield>
        <s:textfield name="posttime" label="公布时间"></s:textfield>
        <s:textfield name="content" label="新闻内容"></s:textfield>
        <s:submit value="录入"></s:submit>
    </s:form>
</body>
</html>

录入界面:
这里写图片描写叙述

新闻列表页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻列表</title>
<style type="text/css">
body {
    margin: 0px;
    padding: 0px;
    width: 800px;
    margin: 30px auto;
}

.newslist h2 {
    text-align: center;
}

.newsinfo {
    text-align: center;
}

.newscontent {
    text-indent: 2em;
}

.newscontent  a {
    display: block;
    float: right;
}
</style>
</head>
<body>
    <s:iterator value="#request.newslist">
        <div class="newslist">
            <h2>${title}</h2>
            <p class="newsinfo">${author}${source}${posttime}</p>
            <p class="newscontent">${content}</p>
            <hr>
        </div>
    </s:iterator>

</body>
</html>

这里写图片描写叙述

posted on 2017-07-24 14:45  mthoutai  阅读(372)  评论(0编辑  收藏  举报