struts+hibernate+spring 开发入门
1.先建张表
用的是mysql数据库..
文章表.
CREATE
TABLE
`t_article` (
`a_id` int ( 11 ) NOT NULL auto_increment,
`a_sort` int ( 11 ) NOT NULL default ' 0 ' ,
`a_title` varchar ( 50 ) default NULL ,
`a_body` text ,
`a_author` varchar ( 11 ) default '' ,
`a_hit` int ( 11 ) NOT NULL default ' 0 ' ,
`c_id` int ( 11 ) default ' 0 ' ,
`a_date` varchar ( 20 ) default NULL ,
PRIMARY KEY (`a_id`)
)
`a_id` int ( 11 ) NOT NULL auto_increment,
`a_sort` int ( 11 ) NOT NULL default ' 0 ' ,
`a_title` varchar ( 50 ) default NULL ,
`a_body` text ,
`a_author` varchar ( 11 ) default '' ,
`a_hit` int ( 11 ) NOT NULL default ' 0 ' ,
`c_id` int ( 11 ) default ' 0 ' ,
`a_date` varchar ( 20 ) default NULL ,
PRIMARY KEY (`a_id`)
)
2:写vo
package
firstspring.vo;
public class articleVO {
private int a_id;
private int a_sort;
private int a_hit;
private int c_id;
private String a_title;
private String a_body;
private String a_author;
private String a_date;
public articleVO() {
}
// 写上get() set()方法
public class articleVO {
private int a_id;
private int a_sort;
private int a_hit;
private int c_id;
private String a_title;
private String a_body;
private String a_author;
private String a_date;
public articleVO() {
}
// 写上get() set()方法
3 写VO映射文件
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name ="firstspring.vo.articleVO" table ="t_article" >
< id name ="a_id" column ="a_id" unsaved-value ="0" >
< generator class ="native" />
</ id >
< property name ="c_id" column ="c_id" />
< property name ="a_title" column ="a_title" />
< property name ="a_sort" column ="a_sort" />
< property name ="a_date" column ="a_date" />
< property name ="a_body" column ="a_body" />
< property name ="a_hit" column ="a_hit" />
< property name ="a_author" column ="a_author" />
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name ="firstspring.vo.articleVO" table ="t_article" >
< id name ="a_id" column ="a_id" unsaved-value ="0" >
< generator class ="native" />
</ id >
< property name ="c_id" column ="c_id" />
< property name ="a_title" column ="a_title" />
< property name ="a_sort" column ="a_sort" />
< property name ="a_date" column ="a_date" />
< property name ="a_body" column ="a_body" />
< property name ="a_hit" column ="a_hit" />
< property name ="a_author" column ="a_author" />
</ class >
</ hibernate-mapping >
4: 写dao层接口和bll层接口
4.1 dao层接口
package
firstspring.idao;
import firstspring.vo.articleVO;
public interface ArticleIdao {
void addArticle(articleVO vo);
}
import firstspring.vo.articleVO;
public interface ArticleIdao {
void addArticle(articleVO vo);
}
4.2 bll层接口
package
firstspring.idao;
import firstspring.vo.articleVO;
public interface IArticleBLL {
void addArticle(articleVO vo);
}
import firstspring.vo.articleVO;
public interface IArticleBLL {
void addArticle(articleVO vo);
}
5:写Articledao类
package
firstspring.dao;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import firstspring.idao.ArticleIdao;
import firstspring.vo.articleVO;
public class ArticleDal extends HibernateDaoSupport implements ArticleIdao {
public ArticleDal() {
}
public void addArticle(articleVO vo)
{
this .getHibernateTemplate().save(vo);
this .getHibernateTemplate().flush();
}
}
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import firstspring.idao.ArticleIdao;
import firstspring.vo.articleVO;
public class ArticleDal extends HibernateDaoSupport implements ArticleIdao {
public ArticleDal() {
}
public void addArticle(articleVO vo)
{
this .getHibernateTemplate().save(vo);
this .getHibernateTemplate().flush();
}
}
6.写Articlebll类
package
firstspring.bll;
import firstspring.idao.ArticleIdao;
import firstspring.idao.IArticleBLL;
import firstspring.vo.articleVO;
public class ArticleBll implements IArticleBLL {
private ArticleIdao dal=null;
public ArticleBll() {
}
public void addArticle(articleVO vo)
{
dal.addArticle(vo);
}
public ArticleIdao getDal() {
return dal;
}
public void setDal(ArticleIdao dal) {
this.dal = dal;
}
}
7:添加struts框架。
import firstspring.idao.ArticleIdao;
import firstspring.idao.IArticleBLL;
import firstspring.vo.articleVO;
public class ArticleBll implements IArticleBLL {
private ArticleIdao dal=null;
public ArticleBll() {
}
public void addArticle(articleVO vo)
{
dal.addArticle(vo);
}
public ArticleIdao getDal() {
return dal;
}
public void setDal(ArticleIdao dal) {
this.dal = dal;
}
}
7.1 建个BaseAction类继承Action
package
firstspring.struts;
import org.apache.struts.action. * ;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
public abstract class BaseAction extends Action {
private WebApplicationContext wac;
public BaseAction() {
}
public void setServlet(ActionServlet actionServlet)
{
super.setServlet(actionServlet);
ServletContext servletContext=actionServlet.getServletContext();
wac=WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//wac.get
}
public WebApplicationContext getweb()
{
return wac;
}
}
8:添加articleForm
import org.apache.struts.action. * ;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
public abstract class BaseAction extends Action {
private WebApplicationContext wac;
public BaseAction() {
}
public void setServlet(ActionServlet actionServlet)
{
super.setServlet(actionServlet);
ServletContext servletContext=actionServlet.getServletContext();
wac=WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//wac.get
}
public WebApplicationContext getweb()
{
return wac;
}
}
package
firstspring.struts;
import javax.servlet. * ;
import javax.servlet.http. * ;
import org.apache.commons.validator. * ;
import org.apache.struts.action. * ;
import org.apache.struts.validator. * ;
public class articleForm extends ValidatorForm {
private String a_auhor;
private String a_body;
private String a_date;
private Integer a_hit;
private Integer a_id;
private Integer a_sort;
private String a_title;
private Integer c_id;
public String getA_auhor() {
return a_auhor;
}
public void setA_auhor(String a_auhor) {
this.a_auhor = a_auhor;
}
public void setC_id(Integer c_id) {
this.c_id = c_id;
}
public void setA_title(String a_title) {
this.a_title = a_title;
}
public void setA_sort(Integer a_sort) {
this.a_sort = a_sort;
}
public void setA_id(Integer a_id) {
this.a_id = a_id;
}
public void setA_hit(Integer a_hit) {
this.a_hit = a_hit;
}
public void setA_date(String a_date) {
this.a_date = a_date;
}
public void setA_body(String a_body) {
this.a_body = a_body;
}
public String getA_body() {
return a_body;
}
public String getA_date() {
return a_date;
}
public Integer getA_hit() {
return a_hit;
}
public Integer getA_id() {
return a_id;
}
public Integer getA_sort() {
return a_sort;
}
public String getA_title() {
return a_title;
}
public Integer getC_id() {
return c_id;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
ServletContext application=this.getServlet().getServletContext();
ActionErrors errors=new ActionErrors();
String validationKey=this.getValidationKey(actionMapping,httpServletRequest);
Validator validator=Resources.initValidator(validationKey,this,application,httpServletRequest,errors,page);//String,Object,ServletContext,HttpServletRequest,ActionMessages,int
try
{
validatorResults=validator.validate();
}
catch(ValidatorException e)
{
//log.error(e.getMessage(),e);
}
return null ;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
9:添加ArticleAction继承刚刚建的BaseAction
import javax.servlet. * ;
import javax.servlet.http. * ;
import org.apache.commons.validator. * ;
import org.apache.struts.action. * ;
import org.apache.struts.validator. * ;
public class articleForm extends ValidatorForm {
private String a_auhor;
private String a_body;
private String a_date;
private Integer a_hit;
private Integer a_id;
private Integer a_sort;
private String a_title;
private Integer c_id;
public String getA_auhor() {
return a_auhor;
}
public void setA_auhor(String a_auhor) {
this.a_auhor = a_auhor;
}
public void setC_id(Integer c_id) {
this.c_id = c_id;
}
public void setA_title(String a_title) {
this.a_title = a_title;
}
public void setA_sort(Integer a_sort) {
this.a_sort = a_sort;
}
public void setA_id(Integer a_id) {
this.a_id = a_id;
}
public void setA_hit(Integer a_hit) {
this.a_hit = a_hit;
}
public void setA_date(String a_date) {
this.a_date = a_date;
}
public void setA_body(String a_body) {
this.a_body = a_body;
}
public String getA_body() {
return a_body;
}
public String getA_date() {
return a_date;
}
public Integer getA_hit() {
return a_hit;
}
public Integer getA_id() {
return a_id;
}
public Integer getA_sort() {
return a_sort;
}
public String getA_title() {
return a_title;
}
public Integer getC_id() {
return c_id;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
ServletContext application=this.getServlet().getServletContext();
ActionErrors errors=new ActionErrors();
String validationKey=this.getValidationKey(actionMapping,httpServletRequest);
Validator validator=Resources.initValidator(validationKey,this,application,httpServletRequest,errors,page);//String,Object,ServletContext,HttpServletRequest,ActionMessages,int
try
{
validatorResults=validator.validate();
}
catch(ValidatorException e)
{
//log.error(e.getMessage(),e);
}
return null ;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
package
firstspring.struts;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import firstspring.vo.articleVO;
import firstspring.idao.IArticleBLL;
public class articleAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//这里有几个数据直接写上了,没有从前台表单取。。
articleForm f = (articleForm) form;
articleVO vo=new articleVO();
vo.setA_author(f.getA_auhor());
vo.setA_body(f.getA_body());
vo.setA_hit(100);
vo.setA_date(f.getA_date());
vo.setA_sort(0);
vo.setC_id(1);
vo.setA_title(f.getA_title());
//IArticleBLL
IArticleBLL ibll=(IArticleBLL)this.getweb().getBean("articlebll");
ibll.addArticle(vo);
return null;
}
}
好了。到这就快差不多了。。还有个重要的东西。写个appcontext.xml配置文件。工厂就在这了。。
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import firstspring.vo.articleVO;
import firstspring.idao.IArticleBLL;
public class articleAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//这里有几个数据直接写上了,没有从前台表单取。。
articleForm f = (articleForm) form;
articleVO vo=new articleVO();
vo.setA_author(f.getA_auhor());
vo.setA_body(f.getA_body());
vo.setA_hit(100);
vo.setA_date(f.getA_date());
vo.setA_sort(0);
vo.setC_id(1);
vo.setA_title(f.getA_title());
//IArticleBLL
IArticleBLL ibll=(IArticleBLL)this.getweb().getBean("articlebll");
ibll.addArticle(vo);
return null;
}
}
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" >< value > org.gjt.mm.mysql.Driver </ value ></ property >
< property name ="url" >< value > jdbc:mysql://localhost:3306/wjcms </ value ></ property >
< property name ="username" >< value > root </ value ></ property >
< property name ="password" >< value > wujun </ value ></ property >
</ bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="mappingResources" >
< list >
< value >
articleVO.hbm.xml
</ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
net.sf.hibernate.dialect.MySQLDialect
</ prop >
< prop key ="hibernate.show_sql" >
true
</ prop >
</ props >
</ property >
</ bean >
< bean id ="articledal" class ="firstspring.dao.ArticleDal" >
< property name ="sessionFactory" >< ref local ="sessionFactory" /></ property >
</ bean >
< bean id ="articlebll" class ="firstspring.bll.ArticleBll" >
< property name ="dal" >< ref local ="articledal" /></ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" >< value > org.gjt.mm.mysql.Driver </ value ></ property >
< property name ="url" >< value > jdbc:mysql://localhost:3306/wjcms </ value ></ property >
< property name ="username" >< value > root </ value ></ property >
< property name ="password" >< value > wujun </ value ></ property >
</ bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="mappingResources" >
< list >
< value >
articleVO.hbm.xml
</ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
net.sf.hibernate.dialect.MySQLDialect
</ prop >
< prop key ="hibernate.show_sql" >
true
</ prop >
</ props >
</ property >
</ bean >
< bean id ="articledal" class ="firstspring.dao.ArticleDal" >
< property name ="sessionFactory" >< ref local ="sessionFactory" /></ property >
</ bean >
< bean id ="articlebll" class ="firstspring.bll.ArticleBll" >
< property name ="dal" >< ref local ="articledal" /></ property >
</ bean >
</ beans >
建个jsp页面。测试添加一条记录看看。这是前台的代码。。
<
html:form
action
="/articleAction.do"
method
="POST"
>
< html:text property ="a_auhor" />
< br >
< html:text property ="a_body" />
< br >
< html:text property ="a_date" />
< br >
< br >
< br >
< br >
< html:text property ="a_title" />
< br >
< br >
< html:submit property ="submit" value ="Submit" />< br >
< html:reset value ="Reset" />
</ html:form >
到这就快差不多了。还有一个小事情,在web.xml里记的加上一句
< html:text property ="a_auhor" />
< br >
< html:text property ="a_body" />
< br >
< html:text property ="a_date" />
< br >
< br >
< br >
< br >
< html:text property ="a_title" />
< br >
< br >
< html:submit property ="submit" value ="Submit" />< br >
< html:reset value ="Reset" />
</ html:form >
<
servlet
>
< servlet-name > context </ servlet-name >
< servlet-class > org.springframework.web.context.ContextLoaderServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-name > context </ servlet-name >
< servlet-class > org.springframework.web.context.ContextLoaderServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
好,运行一下。。OK 。数据进数据库了。
专家多指点。。......