三个配置文件:
strtus.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="login" extends="struts-default">
<action name="login" class="loginaction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
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: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">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/people">
</property>
<property name="username" value="root"></property>
<property name="password" value="1234"></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>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">create-update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lmning/po/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Activates @Transactional for DefaultImageDatabase -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="peopleDao" class="com.lmning.dao.impl.PeopleDaoImpl"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="template">
<ref local="hibernateTemplate"/>
</property>
</bean>
<bean id="loginservice" class="com.lmning.dao.impl.LoginServiceImpl"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="peopoledao">
<ref local="peopleDao" />
</property>
</bean>
<bean id="loginaction" class="com.lmning.action.LoginAction"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="loginservice">
<ref local="loginservice" />
</property>
</bean>
</beans>
LoginAction.java
package com.lmning.action;
import com.lmning.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String name;
private String password;
private LoginService loginservice;
public String execute()throws Exception{
System.out.println("execute()");
boolean flag = loginservice.isExist(name, password);
System.out.println("flag:"+flag);
return "success";
}
@Override
public void validate(){
System.out.println("validate");
System.out.println(this.getName()+"="+password);
if(null==this.getName()||"".equals(this.getName().trim())){
this.addFieldError("name", "name is not validate");
}
if(null==this.getPassword()||"".equals(this.getPassword().trim())){
this.addFieldError("password", "password is not validate");
}
}
public LoginService getLoginservice() {
return loginservice;
}
public void setLoginservice(LoginService loginservice) {
this.loginservice = loginservice;
}
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;
}
}
PeopleDao.java
package com.lmning.dao;
import java.util.List;
import com.lmning.po.User;
public interface PeopleDao {
public abstract void insertUser(User user);
public User getUser(String name,String password);
public List<User> queryAll();
}
PeopleDaoImpl.java
package com.lmning.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.lmning.dao.PeopleDao;
import com.lmning.po.User;
public class PeopleDaoImpl implements PeopleDao {
private HibernateTemplate template;
public void insertUser(User user){
template.save(user);
}
public List<User> queryAll() {
List<User> users = template.find("from User");
return users;
}
public User getUser(String name, String password) {
List<User> user = template.find("from User u where u.name='"+name+"' and u.password='"+password+"'");
if(user==null||user.size()==0)return null;
return user.get(0);
}
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
}
还有domain:
package com.lmning.po;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="people")
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1542547794916127621L;
@Id
@GeneratedValue
private int id;
@Column(name="name",nullable=false)
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
测试 ok!