ssh(Struts+Spring+Hibernate)环境筹建,实现登录
ssh(Struts+Spring+Hibernate)环境搭建,实现登录??我们选择Hibernate3.2,全部按照默认设置即可,然后点击Ne
ssh(Struts+Spring+Hibernate)环境搭建,实现登录
?
?
我们选择Hibernate3.2,全部按照默认设置即可,然后点击Next
?
?
?
在对话框中选择Spring configuration file,表示我们希望将Hibernate托管给Spring进行管理,这是将Hibernate与Spring进行整合的基础。然后点击Next
?
?
?
在出现的对话框中选择Existing Spring configuration file。因为我们已经添加了Spring的配置文件,所以这里选择的是已存在的配置文件。MyEclipse会自动找到存在的那个文件。然后在SessionFactory ID中输入Hibernate的SessionFactory在Spring配置文件的Bean ID的名字,这里我们输入sessionFactory即可。然后点击Next
?
?
在出现的对话框中的Bean Id里面输入数据源在Spring中的Bean ID的名字,这里我们输入dataSource。然后在DB Driver里面选择我们刚刚配置好的ssh,MyEclipse会将其余的信息自动填写到表格里面。然后点击Next
?
?
?
在出现的对话框中取消Create SessionFactory class。点击Finish即可
?
此时,切换到MyEclipse DataBase Explorer视图,右键打开数据库连接,在需要使用的数据表格上面点击右键,选择Hibernate Reverse Engineering...。这里我们使用刚刚建立在test数据库中添加的user表
?
?
?
在打开的对话框中修改Java src folder为我们建立的/ssh/src,这里需要选择到src文件夹,并且需要填写Java package,这是MyEclipse生成的类所在的包,我们将其取名为user。然后选择Java Data Object,建立POJO类。然后选择Java Data Access Object。其中,POJO类是数据库表格所对应的Java类,JDO类是MyEclipse自动生成的对数据库的一些操作。这里会封装一些常用的操作,简化我们的编写。填写完成后点击Next
?
?
?
在出现的对话框中选择ID Generator为native,然后点击Finish即可
?
?
?
?
,右击鼠标——>Refactor——>Extract Interface
[java]?view plaincopy
- ITestService.java??
- package?com.ssh.service;??
- import?java.util.List;??
- public?interface?ITestService?{??
- ??????
- ?????public?abstract?List?findByUsername(Object?username);??
- }??
?
?
[java]?view plaincopy
- UserService.java??
- package?com.ssh.service.impl;??
- import?java.util.List;??
- import?com.ssh.dao.ITestDAO;??
- import?com.ssh.service.ITestService;??
- public?class?TestService?implements?ITestService{??
- ??????
- ????private?ITestDAO?testDAO;??
- ??????
- ????public?List?findByUsername(Object?username){??
- ????????return?testDAO.findByUsername(username);??
- ????}??
- ????public?ITestDAO?getTestDAO()?{??
- ????????return?testDAO;??
- ????}??
- ????public?void?setTestDAO(ITestDAO?testDAO)?{??
- ????????this.testDAO?=?testDAO;??
- ????}??
- ??????
- }??
?
?
[xhtml]?view plaincopy
- <bean?id="UserDAO"?class="com.ssh.dao.impl.UserDAO">??
- ????<property?name="sessionFactory">??
- ???????<ref?bean="sessionFactory"?/>??
- ????</property>??
- </bean>??
?
?
[java]?view plaincopy
- package?com.ssh.struts.action;??
- import?org.springframework.web.context.WebApplicationContext;??
- import?org.springframework.web.context.support.WebApplicationContextUtils;??
- import?org.springframework.web.struts.ActionSupport;??
- public?class?BaseAction?extends?ActionSupport?{??
- protected?Object?getBean(String?id)?{??
- WebApplicationContext?ctx?=?WebApplicationContextUtils??
- .getWebApplicationContext(this.servlet.getServletContext());??
- return?ctx.getBean(id);??
- }??
- }??
?
?
[xhtml]?view plaincopy
- <context-param>??
- <param-name>contextConfigLocation</param-name>??
- <param-value>/WEB-INF/applicationContext.xml</param-value>??
- </context-param>??
- <servlet>??
- <servlet-name>SpringContextServlet</servlet-name>??
- <servlet-class>??
- org.springframework.web.context.ContextLoaderServlet??
- </servlet-class>??
- <load-on-startup>1</load-on-startup>??
- </servlet>??
?
?
[java]?view plaincopy
- public?ActionForward?execute(ActionMapping?mapping,?ActionForm?form,??
- ????????????HttpServletRequest?request,?HttpServletResponse?response)?{??
- ????????????LoginForm?loginForm?=?(LoginForm)?form;??
- ????????????String?username?=?loginForm.getUsername();??
- ????????????String?password?=?loginForm.getPassword();??
- ????????????ActionForward?forward?=?mapping.getInputForward();??
- ????????????ITestService?service?=?(ITestService)?getBean("testService");??
- ????????????Test?userFromDB?=?(Test)service.findByUsername(username).get(0);??
- ????????????if?(userFromDB.getPassword().equals(password))?{??
- ????????????forward?=?mapping.findForward("suc");??
- ????????????}??
- ????????????return?forward;??
- ????????????}??
- }??
?
?
[java]?view plaincopy
- <%@?page?language="java"?pageEncoding="ISO-8859-1"%>??
- <%@?taglib?uri="http://struts.apache.org/tags-bean"?prefix="bean"%>??
- <%@?taglib?uri="http://struts.apache.org/tags-html"?prefix="html"%>??
- <html>??
- <head>??
- <title>JSP?for?LoginForm?form</title>??
- </head>??
- <body>??
- <html:form?action="/login">??
- password?:?<html:password?property="password"/><html:errors?property="password"/><br/>??
- username?:?<html:text?property="username"/><html:errors?property="username"/><br/>??
- <html:submit/><html:cancel/>??
- </html:form>??
- </body>??
- </html>??
?
?
[xhtml]?view plaincopy
- <bean?name="testService"?class="com.ssh.service.impl.TestService">??
- <property?name="testDAO">??
- <ref?bean="TestDAO"?/>??
- </property>??
- </bean>??
?
?
?
?
这样就完成了SSH框架实现的登录功能