SSH整合项目(新闻发布)

来源:互联网 发布:sql 单个join on用法 编辑:程序博客网 时间:2024/06/06 07:22

一个简单的S2SH的整合新闻案例,今天给大家分享一下。该项目使用的ORACLE数据库

+Hibernate+Struts2+Spring 。希望可以对大家起到帮助

1.oracle创建用户和表


--创建表空间create tablespace superhang loggingdatafile 'd:/superhang.dbf'size 32mautoextend onnext 32m maxsize 2048mextent management local;--创建临时表空间create temporary tablespace bocodbtempdbs tempfile 'd:/bocodbtempdbs01.dbf' size 32m autoextend on next 32m maxsize 2048mextent management local;create user xuhang identified by svse default tablespace superhangtemporary tablespace bocodbtempdbs;--授权grant connect,resource to xuhang;--查看所有表空间select dbf.tablespace_name,dbf.totalspace "总量(M)",dbf.totalblocks as 总块数,dfs.freespace "剩余总量(M)",dfs.freeblocks "剩余块数",(dfs.freespace / dbf.totalspace) * 100 "空闲比例" from (select t.tablespace_name,sum(t.bytes) / 1024 / 1024 totalspace,sum(t.blocks) totalblocksfrom dba_data_files tgroup by t.tablespace_name) dbf,(select tt.tablespace_name,sum(tt.bytes) / 1024 / 1024 freespace,sum(tt.blocks) freeblocksfrom dba_free_space ttgroup by tt.tablespace_name) dfswhere trim(dbf.tablespace_name) = trim(dfs.tablespace_name)

--创建news表create table news(nid number  primary key ,kid number references userinfo(id),ntitle varchar2(50)  not null,ncontext varchar2(500)  not null,ntime varchar2(50)  not null)--创建发布人表create table userinfo( id number primary key, uname varchar2(50) not null, upsw varchar2(50) not null, urealname varchar2(50) not null      )--创建序列create sequence user_seqminvalue 1maxvalue 999999999start with 1increment by 1cache 20; --创建序列create sequence news_seqminvalue 1maxvalue 999999999start with 1increment by 1cache 20; insert into userinfo values (user_seq.Nextval,'admin','123456','张三');insert into news values(news_seq.Nextval,2,'张三成功的登上了月球','月球,俗称月亮,古时又称太阴、玄兔,是地球唯一的天然卫星,并且是太阳系中第五大的卫星','2014-5-21');insert into news values(news_seq.Nextval,2,'世界末日什么时候来','xxxxx','2014-5-22');insert into news values(news_seq.Nextval,2,'amazeUI很不错','xxxxxxxx','2014-5-23');select * from userinfo;select * from news;


2.搭建SSH的环境

导入时Spring时勾选这5个包

导入Struts时勾选这2个包


导入hibernate勾选这2个包



3.项目搭建好了。我们就可以编写业务逻辑了

web.xml文件加入2(listener和filter)个配置

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 全局上下文参数 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param>  <filter>  <filter-name>struts2</filter-name>  <filter-class>  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  </filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

--dao层,使用接口的编写方式,定义2个接口

package xander.dao;import java.util.List;import xander.entity.News;public interface NewsDAO {public List<News> getAllNews();public void addNews(News news);}
package xander.dao;
import xander.entity.Userinfo;public interface UserDAO {public Userinfo getUser(String uname,String upsw);}

--2接口的实现类DAOImpl
package xander.dao.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.stereotype.Repository;import xander.dao.NewsDAO;import xander.entity.News;@Repository("newsDAO")public class NewsDAOImpl implements NewsDAO{@Resourceprivate HibernateTemplate hibernateTemplate;public void addNews(News news) {hibernateTemplate.save(news);}public List<News> getAllNews() {List<News> news =hibernateTemplate.find("from News");return news;}}
package xander.dao.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.stereotype.Repository;import xander.dao.UserDAO;import xander.entity.Userinfo;@Repository("userDAO")public class UserDAOImpl implements UserDAO{@Resourceprivate HibernateTemplate hibernateTemplate;public Userinfo getUser(String uname, String upsw) {String hql = "from Userinfo u where u.uname = ? and u.upsw = ?";List<Userinfo> userinfos = hibernateTemplate.find(hql, uname,upsw);if(userinfos==null || userinfos.size()==0){return null;}return userinfos.get(0);}}


--service层也是使用2个接口
package xander.service;import java.util.List;import xander.entity.News;public interface NewsService {public List<News> getAllNews();public void addNews(News news);}

package xander.service;import xander.entity.Userinfo;public interface UserService {public Userinfo getUser(String uname,String upsw);}

--service接口的实现类
package xander.service;import xander.entity.Userinfo;public interface UserService {public Userinfo getUser(String uname,String upsw);}

package xander.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import xander.dao.UserDAO;import xander.entity.Userinfo;import xander.service.UserService;@Service("userService")public class UserServiceImpl implements UserService{@Resourceprivate UserDAO userDAO;public Userinfo getUser(String uname, String upsw) {// TODO Auto-generated method stubreturn userDAO.getUser(uname, upsw);}}


--接下里我们就可以编写action类
一个UserAction处理登录
package xander.action;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import javax.annotation.Resource;import javax.xml.crypto.Data;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import xander.entity.News;import xander.service.NewsService;import com.opensymphony.xwork2.ActionSupport;@Controller("newsAction")@Scope("prototype")public class NewsAction extends ActionSupport {private News snews;@Resourceprivate NewsService newsService;private List<News> news;public News getSnews() {return snews;}public void setSnews(News snews) {this.snews = snews;}public List<News> getNews() {return news;}public void setNews(List<News> news) {this.news = news;}public String addUI() throws Exception {return "addUI";}public String add() throws Exception {Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String ntime =sdf.format(date);snews.setNtime(ntime);newsService.addNews(snews);// 获取新闻列表news = newsService.getAllNews();return "list";}}

--NewsAction处理新闻的显示
package xander.action;import java.util.List;import java.util.Map;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import xander.entity.News;import xander.entity.Userinfo;import xander.service.NewsService;import xander.service.UserService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;@Controller("userAction")@Scope("prototype")public class UserAction extends ActionSupport{@Resourceprivate UserService userService;@Resourceprivate NewsService newsService;private List<News> news;public List<News> getNews() {return news;}public void setNews(List<News> news) {this.news = news;}private Userinfo userinfo;public Userinfo getUserinfo() {return userinfo;}public void setUserinfo(Userinfo userinfo) {this.userinfo = userinfo;}//登录public String login() throws Exception {Userinfo user = userService.getUser(userinfo.getUname(), userinfo.getUpsw());if(user !=null){Map map = ActionContext.getContext().getSession();map.put("user", user);news = newsService.getAllNews();return "list";}else{ActionContext.getContext().put("msg", "你输入的用户名或密码有误!");return INPUT;}}}




import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import javax.annotation.Resource;
import javax.xml.crypto.Data;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


import xander.entity.News;
import xander.service.NewsService;


import com.opensymphony.xwork2.ActionSupport;


@Controller("newsAction")
@Scope("prototype")
public class NewsAction extends ActionSupport {
private News snews;
@Resource
private NewsService newsService;

private List<News> news;

public News getSnews() {
return snews;
}


public void setSnews(News snews) {
this.snews = snews;
}


public List<News> getNews() {
return news;
}


public void setNews(List<News> news) {
this.news = news;
}


public String addUI() throws Exception {

return "addUI";
}

public String add() throws Exception {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String ntime =sdf.format(date);

snews.setNtime(ntime);

newsService.addNews(snews);
// 获取新闻列表
news = newsService.getAllNews();
return "list";
}


}
--struts的配置文件如下
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><!-- 将struts对象交给spring容器 --><constant name="struts.objectFactory" value="spring"></constant><package name="user" extends="struts-default" namespace="/user"><action name="user_*" class="userAction" method="{1}"><result name="list">/show_news.jsp</result><result name="input">/index.jsp</result></action></package><package name="news" extends="struts-default" namespace="/news"><action name="news_*" class="newsAction" method="{1}"><result name="addUI">/news_addUI.jsp</result><result name="list">/show_news.jsp</result></action></package></struts>    
--applicationContext配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "><context:component-scan base-package="xander"></context:component-scan><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName"value="oracle.jdbc.driver.OracleDriver"></property><property name="url"value="jdbc:oracle:thin:@localhost:1521:ORCL"></property><property name="username" value="xuhang"></property><property name="password" value="svse"></property></bean><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" ><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop></props></property><property name="mappingResources"><list><value>xander/entity/News.hbm.xml</value><value>xander/entity/Userinfo.hbm.xml</value></list></property></bean></beans>


   
--3个JSP页面如下
1.登录页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html>  <head> <style type="text/css">       .border-table {           border-collapse: collapse;           border: 1px;       }      .border-table tr{      border: solid red 1px;        }       .border-table th {           border: solid red 1px;       }   </style>         <title>登录页面</title>  </head>    <body>   <form action="<%=request.getContextPath()%>/user/user_login.action" method="post" name="myform">   <table style="border: 1px solid red;" class="border-table">    <thead>    <tr>      <th colspan="2" align="center" style="border: 1px solid red;">新闻发布系统后台管理</th>    </tr>  </thead>   <tbody>   <tr>   <th style="border: 1px solid red;">帐号:</th>   <th style="border: 1px solid red;"><input type="text" name="userinfo.uname"></th>   </tr>   <tr>   <th style="border: 1px solid red;">密码:</th>   <th style="border: 1px solid red;"><input type="password" name="userinfo.upsw" ></th>   </tr>   </tbody>   <tfoot>   <tr>   <th colspan="2" align="center"><input type="submit" value="提交" style="border: 1px solid red;"> </th>   </tr>     </tfoot>      </table>   </form>  </body></html>

--新闻显示页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags" %><html>  <head>    <title>显示新闻信息</title>     <style type="text/css">       .border-table {           border-collapse: collapse;           border: 1px;       }      .border-table tr{      border: solid red 1px;        }       .border-table th {           border: solid red 1px;       }   </style>   </head>    <body>   <table class="border-table">   <thead>   <tr style="border: 1px solid red;">   <th>编号</th>   <th>标题</th>   <th>发布时间</th>   <th>发布人</th>   </tr>   </thead>      <tbody><s:iterator value="news"><tr><th><s:property value="nid"/> </th><th><s:property value="ntitle"/></th><th><s:property value="ntime"/></th><th>${sessionScope.user.urealname}</th></tr></s:iterator>      </tbody>    <tfoot>  <tr>  <th colspan="4" align="center"><a href="<%=request.getContextPath()%>/news/news_addUI.action">发布新闻</a> </th>  </tr>  </tfoot>   </table>  </body></html>
--新闻添加页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>           <title>添加新闻页面</title>      </head>    <body>    <s:form action="news_add.action" method="post" namespace="/news">    <s:textfield name="snews.ntitle" label="标题"></s:textfield>    <s:textarea name="snews.ncontext" label="内容" cols="60" rows="5" ></s:textarea>    <s:textfield label="发布人" value="%{#session.user.urealname}" disabled="true"></s:textfield>    <s:hidden name="snews.userinfo.id" value="%{#session.user.id}" ></s:hidden>    <s:submit value="提交"></s:submit>    </s:form>  </body></html>


完成之后的效果:
到这里我们的项目就做完了,一个简易新闻SSH整合就结束了,我们使用了部分注解的方式微笑微笑

 
原创粉丝点击