ssh工程步骤.docx

上传人:b****1 文档编号:10515567 上传时间:2023-05-26 格式:DOCX 页数:34 大小:497.08KB
下载 相关 举报
ssh工程步骤.docx_第1页
第1页 / 共34页
ssh工程步骤.docx_第2页
第2页 / 共34页
ssh工程步骤.docx_第3页
第3页 / 共34页
ssh工程步骤.docx_第4页
第4页 / 共34页
ssh工程步骤.docx_第5页
第5页 / 共34页
ssh工程步骤.docx_第6页
第6页 / 共34页
ssh工程步骤.docx_第7页
第7页 / 共34页
ssh工程步骤.docx_第8页
第8页 / 共34页
ssh工程步骤.docx_第9页
第9页 / 共34页
ssh工程步骤.docx_第10页
第10页 / 共34页
ssh工程步骤.docx_第11页
第11页 / 共34页
ssh工程步骤.docx_第12页
第12页 / 共34页
ssh工程步骤.docx_第13页
第13页 / 共34页
ssh工程步骤.docx_第14页
第14页 / 共34页
ssh工程步骤.docx_第15页
第15页 / 共34页
ssh工程步骤.docx_第16页
第16页 / 共34页
ssh工程步骤.docx_第17页
第17页 / 共34页
ssh工程步骤.docx_第18页
第18页 / 共34页
ssh工程步骤.docx_第19页
第19页 / 共34页
ssh工程步骤.docx_第20页
第20页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

ssh工程步骤.docx

《ssh工程步骤.docx》由会员分享,可在线阅读,更多相关《ssh工程步骤.docx(34页珍藏版)》请在冰点文库上搜索。

ssh工程步骤.docx

ssh工程步骤

一、

myEclipse8.5:

安装(略):

最高可提供:

框架:

JavaEE5

Spring2.0

Hibernate3.2

Struts2

二、

A、创建一个新的webproject,

B、配置Hibernate:

1、先配置Hibernate,选择工程,MyEclipse->Capabilities->AddHibernateCapabilities,进入如下页面:

2、选择Hibernate3.2,如上图选择,

3、配置文件选择默认即可,如下图:

4、用spring管理数据库链接配置,这里不选数据库链接配置,如下图:

5、不用创建SessionFactoryclass,如下图:

6、点击finish,即可。

C、配置Spring

1、选择工程,MyEclipse->Capabilities->AddSpringCapabilities,进入如下页面:

选择spring2.0,选中前4个和WebLibraries(也可以直接导入spring.jar,spring.jar包含了所需要的所有jar文件);

选中增加到lib目录

2、spring配置文件,路径选择到WEB-INF下,如下图:

3、不创建SpringSessionFactory,待会手工完成,如下图:

4、点击Finish,完成。

D、配置Struts2.0

1、拷贝struts的支持jar包到lib文件夹,拷贝文件如下:

第四个为Spring支持文件。

E、开始编写代码咯:

1、new一个struts.xml文件,放在/src路径下,如下图:

2、struts.xml配置文件的申明如下:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http:

//struts.apache.org/dtds/struts-2.0.dtd">

如下图:

有了这些申明之后,struts会自动下载struts-2.0.dtd文件,自此struts具有标签提示功能。

3、配置web.xml文件:

3.1、配置过滤器(访问页面的时候开始起作用):

--過濾器-->

struts2

org.apache.struts2.dispatcher.FilterDispatcher

struts2

/*

3.2、配置spring监听器(服务器启动的时候开始起作用):

--監聽器-->

org.springframework.web.context.ContextLoaderListener

至此,框架搭建完成。

五、编写实际应用的代码(以一个简单的登录功能为例):

1、创建一个数据库表testuser:

createtableTESTUSER(

"Id"NUMBER(38)notnull,

"firstname"NVARCHAR2(50),

"lastname"VARCHAR2(50),

"pass"NUMBER(10),

constraint"PK_USER"primarykey("Id")

);

createuniqueindex"SCOTT"."PK_TESTUSER"onTESTUSER("Id");

2、编写操作页面,index.jsp;

在页面上用struts标签,因此首先导入struts标签库:

<%@taglibprefix="s"uri="/struts-tags"%>

代码如下:

页面效果:

3、编写保持用户信息页面save.jsp,代码如下:

user.firstname对应数据库表中的firstname.

4、编写服务器端代码:

创建一个包com.test.bean,在保中常见一个bean类TestUser;

代码如下:

packagecom.bean;

publicclassTestUser

{

privateIntegerid;

privateStringfirstname;

privateStringlastname;

privateintpass;

publicIntegergetId()

{

returnid;

}

publicvoidsetId(Integerid)

{

this.id=id;

}

publicStringgetFirstname()

{

returnfirstname;

}

publicvoidsetFirstname(Stringfirstname)

{

this.firstname=firstname;

}

publicStringgetLastname()

{

returnlastname;

}

publicvoidsetLastname(Stringlastname)

{

this.lastname=lastname;

}

publicintgetPass()

{

returnpass;

}

publicvoidsetPass(intpass)

{

this.pass=pass;

}

}

5、将POJO类TestUser与数据库表对应起来:

创建一个配置文件(手工方式)TestUser.hbm.xml,与TestUser.jave同在一个目录下:

文件中必须声明,如下:

xmlversion='1.0'encoding='UTF-8'?

>

DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"

如图:

6、配置数据库表与bean的映射文件TestUser.hbm.xml:

xmlversion="1.0"encoding="utf-8"?

>

DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"

 

length="50">

length="50">

length="16">

7.紧接着,创建action包com.action.user,在该包中创建一个Action类SaveUserAction,继承ActionSupport,如下图:

该类中的内容如下,在该类中执行保存用户的操作,里面只有一个成员属性user,以及相应的get,set方法,和存储的执行方法execute;在execute方法里面调用业务逻辑层的servlet,servlet在调用DAO执行存储操作。

代码如下:

packagecom.action.user;

importcom.bean.TestUser;

importcom.opensymphony.xwork2.ActionSupport;

publicclassSaveUserActionextendsActionSupport

{

privateTestUseruser;//与save.jsp上的user是对应的。

publicTestUsergetUser()

{

returnuser;

}

publicvoidsetUser(TestUseruser)

{

this.user=user;

}

@Override

publicStringexecute()throwsException

{

//TODOAuto-generatedmethodstub

returnsuper.execute();

}

}

(以上是第0个视频的)

8、实现国际化

a、在src目录下创建属性文件struts.properties(文件名应该是这样).

文件内容如下:

struts.custom.i18n.resources=globalMessages

b、再创建两个属性文件,文件名分别问globalMessages_en.properties和globalMessages_zh.properties.两文件的文件内容如下:

globalMessages_en.properties:

firstname=firstname

lastname=lastname

password=password

 

globalMessages_zh.properties:

firstname=\u59D3\u6C0F

lastname=\u540D\u5B57

password=\u5BC6\u7801

这样就可以适应浏览器的语言了,若浏览器的语言默认问英语,则读的是上面一个文件;若浏览器的语言默认问中文,则读的是下面一个文件。

C、改写save.jsp文件中,改写结果如下:

formaction="saveUser">

textfieldname="user.firstname"label="%{getText('firstname')}">

textfield>

textfieldname="user.lastname"label="%{getText('lastname')}">

textfield>

textfieldname="user.pass"label="%{getText('password')}">

textfield>

submitvalue="提交">

submit>

form>

这样,label的值就是读相应语言配置文件的值了。

国际化实现完毕。

9、实现用户的存储:

a、创建com.dao包,在保重创建对用户信息进行操作的接口和类:

TestUserDao和TestUserDaoImlp,TestUserDaoImlp实现了接口TestUserDao,在TestUserDao声明了对用户信息的各种操作,代码如下:

TestUserDao:

packagecom.dao;

importjava.util.List;

importcom.bean.TestUser;

publicinterfaceTestUserDAO

{

publicvoidsaveTestUser();

publicvoidremoveTestUser(TestUserTestUser);

publicTestUserfindTestUserById(Integerid);

publicListfindAllTestUser();

publicvoidupdateTestUser(TestUserTestUser);

}

在TestUserDaoImpl使用spring的dao实现用户信息的操作;应在TestUserDaoImpl中继承HibernateDaoSupport.代码如下:

TestUserDaoImpl:

packagecom.dao.impl;

importjava.util.List;

importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

importcom.bean.TestUser;

importcom.dao.TestUserDAO;

publicclassTestUserDAOImplextendsHibernateDaoSupportimplementsTestUserDAO

{

@SuppressWarnings("unchecked")

publicListfindAllTestUser()

{

Stringsql="fromTestUsertestuserorderbyuser.iddesc";//testuser为TestUser类的别名

return(List)this.getHibernateTemplate().find(sql);

}

publicTestUserfindTestUserById(Integerid)

{

TestUseruser=(TestUser)this.getHibernateTemplate().get(TestUser.class,id);

returnuser;

}

publicvoidremoveTestUser(TestUsertestUser)

{

this.getHibernateTemplate().delete(testUser);

}

publicvoidsaveTestUser(TestUsertestUser)

{

this.getHibernateTemplate().save(testUser);

}

publicvoidupdateTestUser(TestUsertestUser)

{

this.getHibernateTemplate().update(testUser);

}

}

 

10、还需要些服务层代码,在服务层中的action调用dao中的方法。

a、先创建包com.service,在包中创建接口UserService,并在接口中声明用户的信息操作:

如下:

TestUserService:

packagecom.service;

importjava.util.List;

importcom.bean.TestUser;

publicinterfaceTestUserService

{

publicListfindAll();

publicvoidsave(TestUsertestUser);

publicvoidDelete(TestUsertestUser);

publicTestUserfindById(Integerid);

publicvoidupdate(TestUsertestUser);

}

b、定义TestUserService接口的实现类:

TestUserServiceImpl:

packagecom.service.impl;

importjava.util.List;

importcom.bean.TestUser;

importcom.dao.TestUserDAO;

importcom.service.TestUserService;

publicclassTestUserServiceImplimplementsTestUserService

{

privateTestUserDAOtestUserDAO;//使用Spring的依赖注入

publicTestUserDAOgetTestUserDAO()//使用依赖注入,因此需要get,set方法

{

returntestUserDAO;

}

publicvoidsetTestUserDAO(TestUserDAOtestUserDAO)

{

this.testUserDAO=testUserDAO;

}

publicvoidDelete(TestUsertestUser)

{

this.testUserDAO.removeTestUser(testUser);

}

publicListfindAll()

{

returnthis.testUserDAO.findAllTestUser();

}

publicTestUserfindById(Integerid)

{

returnthis.testUserDAO.findTestUserById(id);

}

publicvoidsave(TestUsertestUser)

{

this.testUserDAO.saveTestUser(testUser);

}

publicvoidupdate(TestUsertestUser)

{

this.testUserDAO.updateTestUser(testUser);

}

}

11、实现action层的操作:

a、在SaveUserAction中增加一个TestUserService属性及get,set方法.

b、定义execute方法,在execute方法中返回SUCCESS代码如下:

@Override

publicStringexecute()throwsException

{

this.testUserService.save(this.user);

returnSUCCESS;

}

12、定义struts.xml配置文件,如下:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http:

//struts.apache.org/dtds/struts-2.0.dtd">

--action的name应该与jsp中的action值对应,class的值是一个别名,但是要与applicationContext.xml中的id一致-->

listUser.action

/saveUser.jsp

(以上是第一个视频的)

13、配置applicationcontext.xml文件,内容如下:

applicationcontext.xml:

xmlversion="1.0"encoding="UTF-8"?

>

xmlns="http:

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

p="http:

//www.springframework.org/schema/p"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd">

class="mons.dbcp.BasicDataSource">

value="com.mysql.jdbc.Driver">

value="jdbc:

mysql:

//localhost:

3306/mytest">

--最大活跃连接数-->

--最大可空闲的连接数-->

--最大等待连接数-->

--事务自动提交设置,每完成一个操作,自动提交,当操作比较复杂时,该值设置为fault-->

<

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2