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文件就不提供了
- 大小: 124.5 KB
分享到:
相关推荐
ssh环境搭建,采用struts2、hibernate4、spring3搭建环境、实现了邮件发送功能、验证码登录功能、上传下载功能、dwr集成、报表导出功能
ssh环境搭建及配置示例ssh环境搭建及配置示例
ssh环境搭建
MyEclips集成ssh环境搭建。一些说明和对整个集成过程的DEMO。
ssh环境搭建及简单的增删改查。内含齐全的jar包
这个里面只有一个整合所需要的jar包,搭建ssh环境所需要的
通过mysql数据库,成功搭建 Myeclipse8.5+flex4.5+Blazeds+SSH环境搭建
Myeclipse8.5+flex4.5+Blazeds+SSH环境搭建.pdf
Myeclipse8.5+flex4.5+Blazeds+SSH环境搭建及实例.pdf
网络上好多Myeclipse8.5+flex4.5+Blazeds+SSH环境搭建及实例,个人实验过程中出现了不少问题,通过从网络上搜索各种资源,最后成功整合,以供大家学习。当然,还有部分内容没有修改完,但至少能够成功运行
eclipse下搭建ssh环境,eclipse下搭建ssh环境
eclipse开发工具,spring+hibernate+struts2,注解配置。
NULL 博文链接:https://rico-cai.iteye.com/blog/1966595