上一篇博客已经配置好ssh的开发环境, 并生成了数据库表,这篇博客实现从数据库里面读取新闻数据到前台这一功能。
### 一、修改applicationContext.xml
把以下代码加到数据库连接配置之后:
~~~
<!-- 配置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>
~~~
如果出现错误提示:
~~~
Error occured processing XML 'org/springframework/transaction/interceptor/TransactionInterceptor'. See Error Log for more
details
~~~
下载[aspectjweaver-1.8.7.jar](http://mvnrepository.com/artifact/org.aspectj/aspectjweaver)和[aopalliance-1.0.jar](http://mvnrepository.com/artifact/aopalliance/aopalliance)加入lib中。注意aop:pointcut属性的service类所在的包名。
### 二、加入dao、service、action
新建包cn.ac.ucas.dao,添加BaseDao、NewsDao.java
BaseDao
~~~
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
~~~
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
~~~
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
~~~
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
~~~
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContent*.xml</param-value>
</context-param>
~~~
在applicationContentBeans.xml添加下面配置:
~~~
<?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
1. 拷贝struts-2.3.24/apps/struts2-blank/WEB-INF/src/java/struts.xml到conf目录下.
1. 添加action
~~~
<action name="news-*" class="newsAction" method="{1}">
<result name="list">/WEB-INF/newslist.jsp</result>
</action>
~~~
### 五、读取新闻到页面
index.jsp添加一行:
~~~
<a href="news-list">news list</a>
~~~
在WEB-INF目录下配置struts结果页:newslist.jsp,和struts.xml中的配置要一致。
~~~
<%@ 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>
~~~
工程目录:
![这里写图片描述](https://box.kancloud.cn/2016-02-26_56cfbdda0cda2.jpg "")
在数据库中添加数据,运行。
![这里写图片描述](https://box.kancloud.cn/2016-02-26_56cfbdda323f3.jpg "")
总结:
1.新建applicationContent-beans.xml文件后不修改web.xml会出现错误
2. 忘记添加struts2-spring-plugin-2.3.24.jar会导致错误
3. 出现错误耐心调试。
下一篇博客会介绍如何使用ueditor富文本编辑器编辑新闻,并存储到数据库中。有错误之处欢迎指出。
源码下载地址:[http://download.csdn.net/detail/napoay/9415185](http://download.csdn.net/detail/napoay/9415185)
- 前言
- [J2EE]java web项目中调用word转html命令行工具
- [J2EE]jsp项目中使用UEditor富文本编辑器
- [J2EE]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- [j2ee]Eclipse搭建SSH开发框架
- Could not open Hibernate Session for transaction
- class org.springframework.web.context.ContextLoaderListener
- [java01]Java基本数据类型
- [java02]运算符
- jsp、javabean学生信息管理系统
- [java03]java字符串
- [ssh新闻发布系统一]搭建开发环境
- [ssh新闻发布系统二] 读取新闻
- [ssh新闻发布系统三]存储新闻
- [ssh新闻发布系统四]使用富文本编辑器发布新闻
- [ssh新闻发布系统五]删除新闻
- struts2 helloworld
- struts请求走向流程
- [java04]java大数类