SSH整合项目(新闻发布)

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

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

1.oracle创建用户和表


--创建表空间
create tablespace superhang 
logging
datafile 'd:/superhang.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;

--创建临时表空间
create temporary tablespace bocodbtempdbs 
tempfile 'd:/bocodbtempdbs01.dbf' 
size 32m 
autoextend on 
next 32m maxsize 2048m
extent management local;

create user xuhang identified by svse 
default tablespace superhang
temporary 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) totalblocks
from dba_data_files t
group by t.tablespace_name) dbf,
(select tt.tablespace_name,
sum(tt.bytes) / 1024 / 1024 freespace,
sum(tt.blocks) freeblocks
from dba_free_space tt
group by tt.tablespace_name) dfs
where 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_seq
minvalue 1
maxvalue 999999999
start with 1
increment by 1
cache 20; 

--创建序列
create sequence news_seq
minvalue 1
maxvalue 999999999
start with 1
increment by 1
cache 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{
	
	@Resource
	private 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{
	@Resource
	private 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{
	
	@Resource
	private UserDAO userDAO;
	public Userinfo getUser(String uname, String upsw) {
		// TODO Auto-generated method stub
		return 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;
	@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";
	}
	
	
}


--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{
	@Resource
	private UserService userService;
	@Resource
	private 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整合就结束了,我们使用了部分注解的方式微笑微笑











  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值