SSH整合中正确配置以及实例

mysql数据库表已经字段:




项目文件结构:






调试过程中要注意的事项:

①:applicationContext.xml文件要放在src目录下

②:容易出错的地方如下代码中字体为红色处


常见错误请参看:http://blog.163.com/jxguo_05/blog/static/71940100201023185653156/     &   http://blog.sina.com.cn/s/blog_6757442e0100xumy.html  &

http://blog.csdn.net/yabushandaxue/article/details/39473593

各工具版本:struts2.3.4 、hibernate 3.6.0  spring3.1.3

jar包:如下:下载地址:http://download.csdn.net/detail/sxf1997/7976797





这里是一个简单的利用ssh整合的页面登陆界面

代码如下:

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" 
		 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"> 
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 
   <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   
  <session-config>
  	<session-timeout>30</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <span style="color:#ff0000;"><package name="Action" namespace="/" extends="struts-default"></span>
         <!-- 登录 -->
        <action name="login" class="Action.LoginServiceAction">
       		<result name="success">/success.jsp</result>
      		<result name="error">/login.jsp</result>
        </action>
    </package>
</struts>

login.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>欢迎登录</h1>

<span style="color:#ff0000;"><s:form action="login.action"  method="post" namespace="/"></span>
	<s:textfield name="username" label="账号"></s:textfield>
	<s:password name="password" label="密码"></s:password>
	<s:submit value="登陆"></s:submit>
</s:form>
</body>
</html>


success.jsp


<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>

<h1>欢迎你,<s:property value="username"/>登陆成功!</h1>
</body>
</html>

登陆页面处理的action


package Action;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import DAO.UserDao;
import PO.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginServiceAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 *   
	 */

	private String username;
	private String password;
	<span style="color:#ff0000;">ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");</span>

	UserDao userDao=(UserDao) ctx.getBean("userDao");
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		//查找账号相符的用户
		List<User>userlist=userDao.findByName(username);
		//使用简化的for语句对集合进行遍历并比较用户的密码
		for(User user : userlist){
			if(user.getPassword().equals(password)){
				return SUCCESS;
			}else {
				return ERROR;
			}
		}
		return ERROR;
	}
	

}


处理方法的接口 ( DAO包)


package DAO;

import java.util.List;

import PO.User;

public interface UserDao {
	/**
	 * 加载User实例
	 * @参数id知道需要加载的User实例的主键值
	 * @return 返回加载的User实例
	 * */
	User get(Integer id);
	
	
	/**
	 * 保存User实例
	 * @参数user知道需要保存的User实例
	 * @return 返回刚刚保存的User实例的标识属性值
	 * */
	
	Integer save(User user);
	
	/**
	 * 根据用户名查找User
	 * @参数那么知道查询的用户名
	 * @return返回用户名对呀的全部User
	 * */
	List<User>findByName(String name);
}

实现接口的类ImplDao.UserImpl


package ImplDao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import DAO.UserDao;
import PO.User;

public class UserDaoImpl implements UserDao {

	//实例化一个HibernateTemplate对象,用于执行持久化操作
	private HibernateTemplate ht=null;
	//Hibernate持久化操作所需SessionFactory
	private SessionFactory sessionFactory=null;
	//用户依赖注入的setter方法
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory=sessionFactory;
	}
	//初始化HibernateTemplate方法
	private HibernateTemplate gethiHibernateTemplate(){
		if(ht==null){
			ht=new HibernateTemplate(sessionFactory);
		}
		return ht;
	}
	
	@Override
	public User get(Integer id) {
		// TODO Auto-generated method stub
		//获取对应表中id为某个值的数据,id为主键索引
		return gethiHibernateTemplate().get(User.class, id);
	}

	@Override
	public Integer save(User user) {
		// TODO Auto-generated method stub
		return (Integer)gethiHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findByName(String name) {
		// TODO Auto-generated method stub
		return (List<User>)gethiHibernateTemplate().find("from User u where u.name=?",name);
	}
	

}

PO(hibernate映射数据表的字段)

package PO;

import java.io.Serializable;

public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6810795086071173792L;
	private Integer intId;
	private String name;
	private String password;
	public User(){}
	public User(Integer intId,String name,String password){
		this.intId=intId;
		this.name=name;
		this.password=password;
	}
	/**
	 * @return the intId
	 */
	public Integer getIntId() {
		return intId;
	}
	/**
	 * @param intId the intId to set
	 */
	public void setIntId(Integer intId) {
		this.intId = intId;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
}


User.hbm.xml(PO对象的映射文件)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="PO.User" table="t_use_info" catalog="test">
		<id name="intId" type="integer">
			<column name="int_id"/>
			<generator class="increment"/>
		</id>
		<property name="name" type="string">
			<column name="name" length="32" not-null="true"/>
		</property>
		<property name="password" type="string">
			<column name="password" length="32" not-null="true"/>
		</property>
	</class>

</hibernate-mapping>

hibernate和spring3的配置(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-3.0.xsd">
<!-- default-autowire="byName" -->
	<!-- 定义数据库数据源 --> 
    <bean id="abcd" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost/test</value>
        </property>
        <property name="username">
            <value>test</value>
        </property>
        <property name="password">
            <value>test</value>
        </property>
    </bean> 
    <!-- 定义会话工厂 ,并注入数据源实例dataSource --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="abcd"/>
        </property>
        <property name="hibernateProperties">
            <props> 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props> 
        </property>
        <property name="mappingResources">
            <list>
                <value>PO/User.hbm.xml</value>
            </list>
        </property>
    </bean>    
	
	<!-- HibernateTemplate类是简化Hibernate数据访问代码的辅助类,可以获取一个session对象 -->
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
		<property name="allowCreate">
			<value>true</value>
		</property>
	</bean>
	
	<!-- 依赖注入 -->
	
	<bean id="userDao" class="ImplDao.UserDaoImpl">
		<!-- 注入持久化操作所需的sessionfactory -->
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
</beans>


登陆效果:







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发哥1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值