简单的一个月之设计实现内容3

Posted erwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的一个月之设计实现内容3相关的知识,希望对你有一定的参考价值。

需求:简单的新闻管理系统,要求能实现简单的增删改查功能

5.action(通过struts与页面交互)

package com.pay.boss.action;


import com.pay.boss.entity.News;
import com.pay.boss.service.NewsService;

/**
 * Title:NewsAction
 * action里面的方法根据页面来写
 */

@SuppressWarnings("serial")
public class NewsAction extends Struts2ActionSupport{  //封装数据,并被继承给NewsAction;可以不继承,但需要自己封装;
    
    //注入service;业务
    private NewsService newsService;
    private News news;
    private Long id;
    private String newsNo;
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void newsAdd(){
        try{
            news = newsService.create(news);
            write("添加成功");//没有异常就返回"添加成功"
        }catch (Throwable e) {
            e.printStackTrace();   //捕获异常;
            write("操作异常");
        }
    }
    
    public String newsDetail(){
        news = newsService.findById(news.getId());
        return SUCCESS;
    }//通过id查找并返回成功
    
    public void newsCheck(){
            news = newsService.findByNewsNo(newsNo);
                if (news == null){
                    write("编号可用");
                }else{
                write("编号已存在");
                }
    }//在这里判断,结果返回给页面,就不需要在页面上进行判断了;
    
    public String getNewsNo() {
        return newsNo;
    }

    public void setNewsNo(String newsNo) {
        this.newsNo = newsNo;
    }

    public String newsUpdate(){   //Update的方法
        news = newsService.update(news);
        return SUCCESS;
    }
    
    public NewsService getNewsService() {
        return newsService;
    }
    public void setNewsService(NewsService newsService){
        this.newsService = newsService;
    }
    
    public News getNews(){
        return news;
    }
    public void setNews(News news){
        this.news = news;
    }

}

6.service接口及其实现,可以放在dao后面写,熟练之后顺序无所谓

NewsService

package com.pay.boss.service;

import com.pay.boss.entity.News;

public interface NewsService {
    
    public News create(News news);  //
    
    public News update(News news);//
    
    public void delete(News news);//
    
    public News findById(Long id);//通过id查
    
    public News findByNewsNo(String newsNo); //通过编号查
    

}

NewsServiceImpl

package com.pay.boss.service.impl;

import java.util.Date;

import com.pay.boss.dao.NewsDao;
import com.pay.boss.entity.News;
import com.pay.boss.service.NewsService;
import com.pay.boss.enums.NewsStatus;

public class NewsServiceImpl implements NewsService {
    
    private NewsDao newsDao;   //注入Dao;
    
    public NewsDao getNewsDao(){
        return newsDao;          
    }
    public void setNewsDao(NewsDao newsDao){
        this.newsDao = newsDao;
    }

    @Override
    public News create(News news) {
        news.setStatus(NewsStatus.INIT);
        news.setCreateTime(new Date());
        return newsDao.create(news);
    } 

    @Override
    public News update(News news) {
        News newsDaoDB = newsDao.findById(news.getId());   /*从数据库中取出东西用以修改*/
        newsDaoDB.setStatus(NewsStatus.INIT);
        newsDaoDB.setTitle(news.getTitle());       
        newsDaoDB.setSummary(news.getSummary());
        newsDaoDB.setContent(news.getContent());
        newsDaoDB.setAuthor(news.getAuthor());
        newsDaoDB.setImgUrl(news.getImgUrl());
        newsDaoDB.setNewsCategory(news.getNewsCategory());
        newsDaoDB.setUpdateTime(new Date());
        
        newsDaoDB = newsDao.update(newsDaoDB);  //将修改后的数据传入数据库;
        
        return newsDaoDB;
    }

    @Override
    public void delete(News news) {
        newsDao.delete(news);   //不需要返回值
    }

    @Override
    public News findById(Long id) {
        
        return newsDao.findById(id);
    }

    @Override
    public News findByNewsNo(String newsNo) {
        
        return newsDao.findByNewsNo(newsNo);
    }
    
}

7.配置spring,struts,hibernate,,

spring-manager  是配置javabean

<!-- 新闻需求  -->
    <bean id="newsAction" class="com.pay.boss.action.NewsAction" singleton="false"></bean>
    <bean id="newsService" class="com.pay.boss.service.impl.NewsServiceImpl"></bean>
    <bean id="newsDao" class="com.pay.boss.dao.impl.NewsDaoImpl"></bean>

spring-valueList  一些sql语句,查询,详细信息时使用

<!-- 新闻列表 -->
                <entry key="newsList">
                    <bean parent="defConfig">
                        <property name="sql">
                            <value>
                            <![CDATA[
                             select ID           as id ,
                                    NEWS_NO      as news_no ,
                                    CREATE_TIME  as create_time ,
                                    UPDATE_TIME  as update_time,
                                    STATUS       as status ,
                                    SUMMARY      as summary,
                                    CONTENT      as content,
                                    IMG_URL      as img_url,
                                    NEWS_CATEGORY    as news_category,
                                    TITLE        as title,
                                    AUTHOR       as author
                               from  news
                              where 1=1
                                  /~id: and id = [id]~/
                                /~newsNo: and news_no =‘[newsNo]‘ ~/
                                /~status: and status = ‘[status]‘~/
                                /~newsCategory: and news_category = ‘[newsCategory]‘~/
                             order by id desc
                            ]]>
                            </value>
                        </property>
                    </bean>
                </entry>

struts

有菜单栏的

<result name="toNewsQuery">/jsp/news/newsQuery.jsp</result> 
<result name="toNewsAdd">/jsp/news/newAdd.jsp</result> <!-- 跳转之前的页面 ,带有菜单-->

没菜单栏的(也是通过按钮点击)

<!-- 新闻需求 -->
        <action name="newsDetail" class="newsAction" method="newsDetail">
            <result>/jsp/news/newsDetail.jsp</result>  
        </action>
        <action name="newsQuery" class="defaultValueListAction">
            <param name="queryId">newsList</param>     <!-- valuelist的东西,需在spring-valuelist里配置 -->
            <result>/jsp/news/newsQueryResult.jsp</result>
        </action>
        <action name="newsAdd" class="newsAction" method="newsAdd">
        </action>
        <action name="toNewsUpdate" class="newsAction" method="newsDetail">
            <result>/jsp/news/newsUpdate.jsp</result>      
        </action>                                                       <!-- 一个直接跳转到页面的路径,一个走action(后台)的路径,本质是一样的 -->
        <action name="newsUpdate" class="newsAction" method="newsUpdate">
            <result>/jsp/news/newsDetail.jsp</result>                     <!--  type="redirect"表示不重复提交 -->
        </action> 
        <action name="newsCheck" class="newsAction" method="newsCheck">
        </action>

hibernate:

<mapping class="com.pay.boss.entity.News" />     <!-- mapping:映射 -->

注意,这里面的entityDao就相当于session Factory,所以直接引用就行,否则需要自己注入session Factory

 

8.一切准备完毕,就跑起来检查异常及错误

9.作为一个刚接触java的新手,你滴任重且道远.此系统作为你第一个完成的东西,里面所包含的知识与习惯需要好好的消化与熟悉.,提醒自己努力提升自己的同时还要勿忘初心;

以上是关于简单的一个月之设计实现内容3的主要内容,如果未能解决你的问题,请参考以下文章

关于侧边导航菜单和片段的一般设计问题

小兔子3个月之后生小兔子的问题

201671010432词频统计软件项目报告

炫酷 CSS 背景效果的 10 个代码片段

使用 BottomAppBar 材料设计组件的片段过渡

java大对象存取的简单实现的代码