1 开发环境
MyEclipse 10
JDK 1.6
Java EE 6.0
Tomcat 7.0.25
Struts 2.3.4
Spring 3.1.1
Hibernate 3.6.10
2 为ssh做好准备
所需jar包:
3、配置struts.xml
<?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="user" extends="struts-default"> <action name="login" class="loginAction"> <result name="success">success.jsp</result> <result name="fail">fail.jsp</result> </action> </package> </struts>
4、配置Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>weiboPortal</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
5、配置applicationContext.xml
applicationContext.xml存在于WEB-INF下,同时将hibernate配置项写到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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 设定数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/weibo_portal" /> <property name="username" value="root" /> <property name="password" value="system" /> <property name="maxActive" value="100" /> <property name="maxIdle" value="30" /> <property name="maxWait" value="500" /> <property name="defaultAutoCommit" value="true" /> </bean> <!-- 设定hibernate的sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <!-- 配置映射文件 --> <property name="mappingResources"> <list> <value>com/huawei/pojo/UserDeveloperAccount.hbm.xml</value> </list> </property> </bean> <bean name="loginAction" class="com.huawei.action.UserDeveloperAccountAction" scope="prototype"> <property name="userService"> <ref bean="userDeveloperAccountService" /> </property> </bean> <bean name="userDeveloperAccountService" class="com.huawei.service.UserDeveloperAccountServiceImpl"> <property name="userDao"> <ref bean="userDao" /> </property> </bean> <bean name="userDao" class="com.huawei.dao.UserDeveloperAccountDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans>
6、测试代码
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <base href="<%=basePath%>"> <title>Sign On</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form action="login" method="post" enctype="multipart/form-data"> <input type="text" id="username" name="username" value="111"/> <input type="password" id="password" name="password" value="111"/> <input type="submit" value="确定"/> </form> </body> </html>
fail.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <s:property value="message"/><br> <a href="${pageContext.request.contextPath}">返回</a> ${sessionScope.sessionUser.username }aaaaa <s:debug></s:debug> </body> </html>
success.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <s:property value="message"/><br> <a href="${pageContext.request.contextPath}">success</a> <s:debug></s:debug> </body> </html>
Action文件:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.huawei.base.BaseAction;
import com.huawei.pojo.UserDeveloperAccount;
import com.huawei.service.UserDeveloperAccountService;
import com.opensymphony.xwork2.ActionContext;
public class UserDeveloperAccountAction extends BaseAction {
private static final long serialVersionUID = 1L;
private transient UserDeveloperAccountService userService = null;
private String username;
private String password;
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 UserDeveloperAccountService getUserService() {
return userService;
}
public void setUserService(UserDeveloperAccountService userService) {
this.userService = userService;
}
@Override
public String execute() throws Exception {
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
ServletActionContext.HTTP_REQUEST);
System.out.println(request.getParameter("username"));
UserDeveloperAccount user = new UserDeveloperAccount(username, password, 0);
UserDeveloperAccount dbUser = this.userService.checkUserExits(user);
if (null != dbUser) {
getSession().put("sessionUser",dbUser);
return "success";
}
return "fail";
}
}
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements SessionAware{
private static final long serialVersionUID = 2499104578106367603L;
public void setSession(Map<String, Object> sessionMap) {
}
public Map<String, Object> getSession(){
return ActionContext.getContext().getSession();
}
public HttpServletRequest getRequest(){
return ServletActionContext.getRequest();
}
}
Dao文件
import com.huawei.pojo.UserDeveloperAccount;
public interface UserDeveloperAccountDao {
/**
* 检查用户是否存在
* */
public UserDeveloperAccount getUserByUserIdAndUserNameExits(UserDeveloperAccount user);
}
package com.huawei.dao;
import java.util.List;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.huawei.pojo.UserDeveloperAccount;
public class UserDeveloperAccountDaoImpl extends HibernateDaoSupport implements UserDeveloperAccountDao {
public final Session getCurrentSession()
{
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
return session;
}
public UserDeveloperAccount getUserByUserIdAndUserNameExits(
UserDeveloperAccount user) {
Session session = this.getCurrentSession();
String hql = "from UserDeveloperAccount where username = " + user.getUsername();
List query = session.createQuery(hql).list();
if(query == null || query.size() == 0){
return null;
}else{
return (UserDeveloperAccount) query.get(0);
}
}
}
Service文件:
import com.huawei.pojo.UserDeveloperAccount;
public interface UserDeveloperAccountService {
public UserDeveloperAccount checkUserExits(UserDeveloperAccount user) throws Exception;
}
import com.huawei.dao.UserDeveloperAccountDao;
import com.huawei.pojo.UserDeveloperAccount;
public class UserDeveloperAccountServiceImpl implements UserDeveloperAccountService {
private UserDeveloperAccountDao userDao = null;
public UserDeveloperAccountDao getUserDao() {
return userDao;
}
public void setUserDao(UserDeveloperAccountDao userDao) {
this.userDao = userDao;
}
public UserDeveloperAccount checkUserExits(UserDeveloperAccount user) throws Exception {
return this.userDao.getUserByUserIdAndUserNameExits(user);
}
}
POJO文件就不提供了