[SSH news release system third day] Store news

Source: Internet
Author: User

first, the storage of news DAO method

New Saveorupdate method for storing news in Newsdao.java

publicvoid  saveOrupdate(News news){         getSession().saveOrUpdate(news);}
II. Storage of news service methods

New Saveorupdate method for storing news in Newsservice.java

publicvoidsaveOrupdate(News news){        newsDao.saveOrupdate(news);}
third, store news action

Let Newsaction implement the Modeldriven and Preparable interfaces, and implement their abstract methods.

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 LongSerialversionuid =1LPrivateMap<string, object> request;PrivateNewsservice Newsservice;PrivateNews model; @Override Public voidSetrequest (map<string, object> arg0) {//TODO auto-generated method stub         This. request = arg0; } Public voidSetnewsservice (Newsservice newsservice) { This. Newsservice = Newsservice; } PublicString list () {//News list, get all newsRequest.put ("Newslist", Newsservice.getallnews ());return "List"; } PublicString Save () {//Store News        //SYSTEM.OUT.PRINTLN (model);Newsservice.saveorupdate (model);return "Save"; } PublicString Post () {//Jump to the release news page        return "POST"; } @Override PublicNews Getmodel () {//TODO auto-generated method stub        returnModel } @Override Public voidPrepare () throws Exception {//TODO auto-generated method stub} Public voidPreparesave () {model =NewNews (); }}
Iv. Configuring Struts

Modify Struts.xml to use the new interceptor stack

< package name="Default" namespace= "/" extends=" Struts-default ">        <!--Define a new interceptor, configure the Alwaysinvokeprepare parameter value of the prepare interceptor stack to false --        <interceptors>            <interceptor-stack name="Sshstack">                <interceptor-ref name="Paramsprepareparamsstack">                    <param name="Prepare.alwaysinvokeprepare">False</param>                </interceptor-ref>            </interceptor-stack>        </interceptors>        <!--Use the new interceptor stack--        <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 >

Access News-post jump to/web-inf/newspost.jsp Publish news, Access News-save store news to database and redirect to News list page.

v. Writing front-end pages

Join a line in index.jsp, click to jump to/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="News title"></S:textfield>        <s:textfield name="Author" label="news author"></S:textfield>        <s:textfield name="source" label="news source"></S:textfield>        <s:textfield name="Posttime" label="Publish Time"></S:textfield>        <s:textfield name="Content" label="News contents"> </S:textfield>        <s:submit value="entry"></s:submit>    </s:form></body></html>

Input interface:

News List page:

<%@ 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>News list</title><style type="Text/css">Body {  margin: 0px;     padding: 0px;     width: + px;  margin: px 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>

[SSH news release system third day] Store news

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.