SSH-简单登陆业务详解,从环境部署到代码编写
一直都是半解半知的情况下动手去实现某些功能,但是竟然也可以成功,
而我发现你花2小时听视频看所谓的理论,不如花2小时直接做个功能出来,
更加深了对此框架的理解,中间遇到问题后,可以上网再查详细的知识点,
这样掌握起来更快更牢固-----一个初级菜鸟的认识
1、struts2+Spring+hibernate环境搭建
jar包:myeclipse加的所有支持都不使用里面的jar,jar统一用我本地的备份,链接:http://pan.baidu.com/s/1hsFZ8PI 密码:xtl4
先创建webProject,填写项目名称,点击finish即可
拷贝jar进WebRoot-WEB-INF-lib目录,webproject是直接会加载这些jar的
我一般会先把所用的package先建好,省事,清晰,action放struts的业务处理,dao持久化操作,service业务层,entity实体类
添加支持struts支持:项目-右键-myeclipse-add struts capabilities
添加Spring支持:项目-右键-myeclipse-add Spring capabilities
在添加hibernate依赖之前,你得先把数据源连接添加好
添加hibernate支持:项目-右键-myeclipse-add hibernate capabilities
生成实体类
OK,环境已好,不过要在web.xml中添加spring支持,否则就会报错SSH框架环境搭建问题:Line: 230 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:230:-1
<!-- Spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
至此,环境已配置好了,可以启动下tomcat看一下,如果没报错,基本上就是OK了
现在来看一下项目的目录结构
其实这里看得出,hibernate与spring已在配置上整合了,我们看一下applicationContext.xml配置
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/role"></property> <property name="username" value="root"></property> <property name="password" value="root"></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.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>entity/User.hbm.xml</value></list> </property></bean> <!-- <bean id="UserDAO" class="entity.UserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> --></beans>
接下来就轮到编码了
(1)jsp页面编写login.jsp和main.jsp
(2)dao层:登陆接口
dao层实现类
package dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import dao.LoginDao; import entity.User; public class LoginDaoImpl extends HibernateDaoSupport implements LoginDao { @Override public boolean login(String username, String password) { List<User> u = this.getHibernateTemplate().find("from User where username=?and password=?", username,password); boolean flag = false; if(u.size() >0){ flag = true; }else{ flag = false; } return flag; } }
(3)service:service.impl
LoginServiceImpl.java
package service.impl; import dao.LoginDao; import service.LoginService; public class LoginServiceImpl implements LoginService { private LoginDao loginDao; public void setLoginDao(LoginDao loginDao) { this.loginDao = loginDao; } @Override public boolean login(String username, String password) { return loginDao.login(username, password); } }
(4)该struts的action了。我们先来配置struts吧
struts.xml,这里的action name名称要与jsp里form的action名称一致
<?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> <package name="default" extends="struts-default" namespace="/"> <action name="login" class="action.LoginAction"> <result name="success">/main.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
开始写LoginAction.jsp
package action; import service.LoginService; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ //service层 private LoginService loginService; //jsp登陆用户名name和密码name private String username; private String password; public LoginService getLoginService() { return loginService; } public void setLoginService(LoginService loginService) { this.loginService = loginService; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login(){ boolean flag = loginService.login(username, password); if(flag){ return SUCCESS; }else{ return INPUT; } } }
以上脚本算是完成了,接下来要添加spring的各层依赖注入了,完整理的applicationContext.xml配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/role"></property> <property name="username" value="root"></property> <property name="password" value="root"></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.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>entity/User.hbm.xml</value></list> </property></bean> <!-- 添加dao层依赖 sessionFactory持久化--> <bean id="userdao" class="dao.impl.LoginDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 添加service层的依赖 --> <bean id="userservice" class="service.impl.LoginServiceImpl"> <property name="loginDao" ref="userdao"></property> </bean> <!-- 添加action层的依赖 --> <bean id="useraction" class="action.LoginAction"> <property name="loginService" ref="userservice"></property> </bean> </beans>
到此为止,所有功能完毕,启动tomcate,还是会报错
SSH框架环境搭建问题:java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required 这里是解决方案
最后,运行
最后奉上源码:
链接:http://pan.baidu.com/s/1c2w4XiC 密码:gk3v