[ssh新闻发布系统第二天] 读取新闻
2016-01-23 10:06
417 查看
上一篇博客已经配置好ssh的开发环境, 并生成了数据库表,这篇博客实现从数据库里面读取新闻数据到前台这一功能。
如果出现错误提示:
下载aspectjweaver-1.8.7.jar和aopalliance-1.0.jar加入lib中。注意aop:pointcut属性的service类所在的包名。
BaseDao
NewsDao
新建cn.ac.ucas.service包,添加NewsService.java
新建cn.ac.ucas.action包,添加NewsAction.java
在conf目录下新建applicationContentBeans.xml,打开web.xml,把spring配置的类路径修改,使其能加载applicationContent.xml和applicationContentBeans.xml
在applicationContentBeans.xml添加下面配置:
添加action
在WEB-INF目录下配置struts结果页:newslist.jsp,和struts.xml中的配置要一致。
工程目录:
在数据库中添加数据,运行。
总结:
1.新建applicationContent-beans.xml文件后不修改web.xml会出现错误
2. 忘记添加struts2-spring-plugin-2.3.24.jar会导致错误
3. 出现错误耐心调试。
下一篇博客会介绍如何使用ueditor富文本编辑器编辑新闻,并存储到数据库中。有错误之处欢迎指出。
源码下载地址:http://download.csdn.net/detail/napoay/9415185
一、修改applicationContext.xml
把以下代码加到数据库连接配置之后:[code] <!-- 配置spring的声明式事务 --> <!-- 1.配置hibernate的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2.配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="lastNameIsValid" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 3.配置事务切入点,再把事务属性和事务切入点关联起来 --> <aop:config> <aop:pointcut expression="execution(* cn.ac.ucas.service.*.*(..))" id="txPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config>
如果出现错误提示:
[code]Error occured processing XML 'org/springframework/transaction/interceptor/TransactionInterceptor'. See Error Log for more details
下载aspectjweaver-1.8.7.jar和aopalliance-1.0.jar加入lib中。注意aop:pointcut属性的service类所在的包名。
二、加入dao、service、action
新建包cn.ac.ucas.dao,添加BaseDao、NewsDao.javaBaseDao
[code]package cn.ac.ucas.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; public class BaseDao { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Session getSession(){ return this.sessionFactory.getCurrentSession(); } }
NewsDao
[code]package cn.ac.ucas.dao; import java.util.List; import cn.ac.ucas.form.News; public class NewsDao extends BaseDao { public List<News> getAllnews() { String hql = "FROM News"; return getSession().createQuery(hql).list(); } }
新建cn.ac.ucas.service包,添加NewsService.java
[code]package cn.ac.ucas.service; import java.util.List; import cn.ac.ucas.dao.NewsDao; import cn.ac.ucas.form.News; public class NewsService { private NewsDao newsDao; public void setNewsDao(NewsDao newsDao) { this.newsDao = newsDao; } public List<News> getAllNews(){ return newsDao.getAllnews(); } }
新建cn.ac.ucas.action包,添加NewsAction.java
[code]package cn.ac.ucas.action; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import com.opensymphony.xwork2.ActionSupport; import cn.ac.ucas.service.NewsService; public class NewsAction extends ActionSupport implements RequestAware { /** * */ private static final long serialVersionUID = 1L; private Map<String, Object> request; private NewsService newsService; @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"; } }
三、配置spring
到struts-2.3.24-all/struts-2.3.24/lib找到struts2-spring-plugin-2.3.24.jar,加到lib目录中。在conf目录下新建applicationContentBeans.xml,打开web.xml,把spring配置的类路径修改,使其能加载applicationContent.xml和applicationContentBeans.xml
[code]<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContent*.xml</param-value> </context-param>
在applicationContentBeans.xml添加下面配置:
[code]<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="newsDao" class="cn.ac.ucas.dao.NewsDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="newsService" class="cn.ac.ucas.service.NewsService"> <property name="newsDao" ref="newsDao"></property> </bean> <bean id="newsAction" class="cn.ac.ucas.action.NewsAction"> <property name="newsService" ref="newsService"></property> </bean> </beans>
四、配置struts2
拷贝struts-2.3.24/apps/struts2-blank/WEB-INF/src/java/struts.xml到conf目录下.添加action
[code] <action name="news-*" class="newsAction" method="{1}"> <result name="list">/WEB-INF/newslist.jsp</result> </action>
五、读取新闻到页面
index.jsp添加一行:[code]<a href="news-list">news list</a>
在WEB-INF目录下配置struts结果页:newslist.jsp,和struts.xml中的配置要一致。
[code]<%@ 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:iterator value="#request.newslist"> <h3>${title}</h3> <p>${author}${source} ${posttime}</p> <p>${content}</p> </s:iterator> </body> </html>
工程目录:
在数据库中添加数据,运行。
总结:
1.新建applicationContent-beans.xml文件后不修改web.xml会出现错误
2. 忘记添加struts2-spring-plugin-2.3.24.jar会导致错误
3. 出现错误耐心调试。
下一篇博客会介绍如何使用ueditor富文本编辑器编辑新闻,并存储到数据库中。有错误之处欢迎指出。
源码下载地址:http://download.csdn.net/detail/napoay/9415185
相关文章推荐
- MySQL - 建库、建表、查询
- Atom编辑器中文汉化方法
- linux下的hiredis的安装和使用(一)
- 有关拍照并获取图片显示出来的问题
- AutoCAD.Net/C#.Net QQ群:193522571 从已有A.DWG中复制块BLK到新DWG中的方法
- 生活中的幽默!有趣的纸上立体插画艺术
- iOS UITableView全面解析 (转载)
- Nginx性能优化
- 字,字长,字节
- 理论: 二分查找(1):基础样例
- 使用history.replaceState 控制微信返回
- 互联网协议入门(一)
- NopCommerce添加新功能“类型属性”【新增实体类】
- 线程基础:线程池(5)——基本使用(上)
- Html5 Egret游戏开发 成语大挑战(二)干净的eui项目和资源准备
- 分享提高ASP.NET Web应用性能的技巧
- Swift Function函数
- eclispe格式化html变丑问题!
- 1.20
- 优化项