上一篇博客已經配置好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>
~~~
工程目錄:

在數據庫中添加數據,運行。

總結:
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大數類