实现最简单的登录操作。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts 过滤器 -->
<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>
<!-- 使用spring解决struts2乱码问题。 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
sping配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 定义数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 指定数据库驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库URL -->
<property name="url" value="jdbc:mysql://localhost:3306/ssi" />
<!-- 指定用户名 -->
<property name="username" value="root" />
<!-- 指定密码 -->
<property name="password" value="root" />
</bean>
<!-- 集成ibatIS -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sql-map-config.xml" />
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource" />
</bean>
<!-- 需要引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明了在所有方法产生事务拦截操作 -->
<aop:pointcut id="serviceMethods"
expression="execution(* com.ssi.service.*.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置业务Bean -->
<bean id="userDao" class="com.ssi.dao.impl.UserDaoImpl">
<!-- 注入ibatis -->
<property name="templet" ref="sqlMapClient"/>
</bean>
<bean id="userService" class="com.ssi.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.ssi.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>
struts配置文件
<?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>
<constant name="struts.i18n.encoding" value="utf-8"/>
<constant name="struts.objectFactory" value="spring" />
<include file="struts-default.xml"/>
<package name="default" extends="struts-default">
<!-- class为在spring中配置的action bean的名称 -->
<action name="userLogin" class="userAction" method="userLogin" >
<result name="success">/welcome.jsp</result>
<result name="loginok">/welcome.jsp</result>
</action>
</package>
</struts>
ibatis配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<sqlMap resource="com/ssi/bean/userBean.xml"/>
</sqlMapConfig>
实体bean
package com.ssi.bean;
public class UserBean {
private int id;
private String name;
private String password;
public UserBean(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
ibatis 实体bean配置
关于ibatis配置的说明,参见http://blog.csdn.net/mchange/article/details/8845548
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="User">
<typeAlias type="com.ssi.bean.UserBean" alias="user"/>
<resultMap id="result" class="user" >
<result column="id" property="id" />
<result column="name" property="name" />
<result column="password" property="password" />
</resultMap>
<!-- 查询条件-->
<parameterMap class="java.util.HashMap" id="findUserParam">
<parameter property="name"/>
<parameter property="password"/>
</parameterMap>
<!-- 按用户名密码查询 -->
<select id="findUser" parameterMap="findUserParam" resultMap="result">
select id,name,password from user where name=? and password=?
</select>
</sqlMap>
dao
package com.ssi.dao.impl;
import java.sql.SQLException;
import java.util.HashMap;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ssi.bean.UserBean;
import com.ssi.dao.UserDao;
public class UserDaoImpl implements UserDao{
private SqlMapClient templet;
@Override
public UserBean getUser(String name, String password) {
// TODO Auto-generated method stub
UserBean user = null;
try {
HashMap params = new HashMap();
params.put("name", name);
params.put("password", password);
user = (UserBean)templet.queryForObject("User.findUser",params); //查询
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
public SqlMapClient getTemplet() {
return templet;
}
public void setTemplet(SqlMapClient templet) {
this.templet = templet;
}
}
service
package com.ssi.service.impl;
import com.ssi.bean.UserBean;
import com.ssi.dao.UserDao;
import com.ssi.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
@Override
public UserBean findUser(String name, String password) {
// TODO Auto-generated method stub
return userDao.getUser(name, password);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
action
package com.ssi.action;
import com.opensymphony.xwork2.ActionSupport;
import com.ssi.bean.UserBean;
import com.ssi.service.UserService;
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String password;
private UserBean user;
private UserService userService;
public String userLogin(){
UserBean user = userService.findUser(name, password);
if(null != user){
System.out.println(user.getName());
return SUCCESS;
}
return "regok";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
}
数据库结构
mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | | |
| password | varchar(50) | NO | | | |
+----------+-------------+------+-----+---------+----------------+
项目下载地址(包含全部jar包)
http://download.csdn.net/detail/mchange/5296264