1. 创建一个WEB工程
2. 加入Struts 2核心包
struts2-core-2.0.14.jar
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
xwork-2.0.7.jar
struts2-spring-plugin-2.0.14.jar
3. 加载Spring 2.5选择以下包
Spring 2.5 AOP Libraries
Spring 2.5 Core Libraries
Spring 2.5 Persistence Core Libraries
Spring 2.5 PersistenceJDBC Libraries
Spring 2.5 Web Libraries
选择spring配置文件存储位置为WEB-INF文件夹
4. 加载Hibernate 3.1
包全部选择
下一步 选择Existing Spring configuration file
在SessionFactory Id后 输入: sessionFactory
下一步 选择配置好的数据库连接
finish后有一个keep exting 和replace
选择replace
5. 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>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 开启监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Struts2 配置 -->
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
6. 在src下建struts.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>
<!--导入struts-default.xml文件 -->
<include file="struts-default.xml"></include>
<!-- 配置中文支持 -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<!-- 配置Action -->
<package name="struts" extends="struts-default" namespace="/">
<action name="login" class="loginAction">
<result name="success">/result.jsp</result>
<result name="fail">/error.jsp</result>
</action>
</package>
</struts>
7. 创建数据表
create table `mytest`.`person`(
`id` INT default '' not null,
`uname` VARCHAR(20),
`upwd` VARCHAR(20)
);
8. 使用工具通过Person表生成类文件和Person.hbm.xml
package com.user.vo;
public class Person implements java.io.Serializable {
private Integer id;
private String uname;
private String upwd;
public Person() {
}
public Person(Integer id) {
this.id = id;
}
public Person(Integer id, String uname, String upwd) {
this.id = id;
this.uname = uname;
this.upwd = upwd;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return this.uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return this.upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
<?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="com.user.vo.Person" table="person" catalog="mytest">
<id name="id" type="java.lang.Integer">
<column name="id" length="4" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" length="20" />
</property>
<property name="upwd" type="java.lang.String">
<column name="upwd" length="20" />
</property>
</class>
</hibernate-mapping>
9. 创建业务逻辑处理类
package com.user.dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.user.vo.Person;
public class PersonManager extends HibernateDaoSupport{
public boolean isLogin(Person per){
boolean flag = false;
String hql = "From Person as p where p.uname=? and p.upwd=?";
Query q = this.getSession().createQuery(hql);
q.setString(0, per.getUname());
q.setString(1, per.getUpwd());
List all = q.list();
if(all.size() > 0){
flag = true;
}
else{
flag = false;
}
return flag;
}
}
10. 创建Action
package com.user.action;
import com.opensymphony.xwork2.ActionSupport;
import com.user.dao.PersonManager;
import com.user.vo.Person;
public class LoginAction extends ActionSupport{
private String username;
private String password;
private PersonManager personManager;
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 PersonManager getPersonManager() {
return personManager;
}
public void setPersonManager(PersonManager personManager) {
this.personManager = personManager;
}
@Override
public String execute() throws Exception {
String flag = "";
Person per = new Person();
per.setUname(this.username);
per.setUpwd(this.password);
if(personManager.isLogin(per)){
flag = "success";
}
else{
flag = "fail";
}
return flag;
}
}
11. 配置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-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="org.gjt.mm.mysql.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></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>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/user/vo/Person.hbm.xml</value></list>
</property>
</bean>
<!-- 配置PersonManager.java类文件 -->
<bean id="personManager" class="com.user.dao.PersonManager" abstract="false" lazy-init="default" autowire="default" dependency-check="default">
<property name="sessionFactory" >
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置LoginAction.java类文件 -->
<bean id="loginAction" class="com.user.action.LoginAction" >
<property name="personManager">
<ref bean="personManager" />
</property>
</bean>
</beans>
12. 前台页面
login.jsp
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<s:form action="login" method="POST" namespace="/">
<s:textfield name="username" label="用户名" size="14"/>
<s:password name="password" label="密 码" size="14"/>
<s:submit label="提交" />
</s:form>
</center>
</body>
</html>
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<s:form action="login" method="POST" namespace="/">
<s:textfield name="username" label="用户名" size="14"/>
<s:password name="password" label="密 码" size="14"/>
<s:submit label="提交" />
</s:form>
</center>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%
out.println("输入用误,输入的信息为" + "</br>");
out.println("用户名: " + request.getParameter("username") + "</br>");
out.println("密 码: " + request.getParameter("password"));
%>
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%
out.println("输入用误,输入的信息为" + "</br>");
out.println("用户名: " + request.getParameter("username") + "</br>");
out.println("密 码: " + request.getParameter("password"));
%>
result.jsp
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%
out.println("用户名是: " + request.getParameter("username") + "</br>");
out.println("密 码是: " + request.getParameter("password"));
2. 加入Struts 2核心包
struts2-core-2.0.14.jar
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
xwork-2.0.7.jar
struts2-spring-plugin-2.0.14.jar
3. 加载Spring 2.5选择以下包
Spring 2.5 AOP Libraries
Spring 2.5 Core Libraries
Spring 2.5 Persistence Core Libraries
Spring 2.5 PersistenceJDBC Libraries
Spring 2.5 Web Libraries
选择spring配置文件存储位置为WEB-INF文件夹
4. 加载Hibernate 3.1
包全部选择
下一步 选择Existing Spring configuration file
在SessionFactory Id后 输入: sessionFactory
下一步 选择配置好的数据库连接
finish后有一个keep exting 和replace
选择replace
5. 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>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 开启监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Struts2 配置 -->
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
6. 在src下建struts.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>
<!--导入struts-default.xml文件 -->
<include file="struts-default.xml"></include>
<!-- 配置中文支持 -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<!-- 配置Action -->
<package name="struts" extends="struts-default" namespace="/">
<action name="login" class="loginAction">
<result name="success">/result.jsp</result>
<result name="fail">/error.jsp</result>
</action>
</package>
</struts>
7. 创建数据表
create table `mytest`.`person`(
`id` INT default '' not null,
`uname` VARCHAR(20),
`upwd` VARCHAR(20)
);
8. 使用工具通过Person表生成类文件和Person.hbm.xml
package com.user.vo;
public class Person implements java.io.Serializable {
private Integer id;
private String uname;
private String upwd;
public Person() {
}
public Person(Integer id) {
this.id = id;
}
public Person(Integer id, String uname, String upwd) {
this.id = id;
this.uname = uname;
this.upwd = upwd;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return this.uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return this.upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
<?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="com.user.vo.Person" table="person" catalog="mytest">
<id name="id" type="java.lang.Integer">
<column name="id" length="4" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" length="20" />
</property>
<property name="upwd" type="java.lang.String">
<column name="upwd" length="20" />
</property>
</class>
</hibernate-mapping>
9. 创建业务逻辑处理类
package com.user.dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.user.vo.Person;
public class PersonManager extends HibernateDaoSupport{
public boolean isLogin(Person per){
boolean flag = false;
String hql = "From Person as p where p.uname=? and p.upwd=?";
Query q = this.getSession().createQuery(hql);
q.setString(0, per.getUname());
q.setString(1, per.getUpwd());
List all = q.list();
if(all.size() > 0){
flag = true;
}
else{
flag = false;
}
return flag;
}
}
10. 创建Action
package com.user.action;
import com.opensymphony.xwork2.ActionSupport;
import com.user.dao.PersonManager;
import com.user.vo.Person;
public class LoginAction extends ActionSupport{
private String username;
private String password;
private PersonManager personManager;
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 PersonManager getPersonManager() {
return personManager;
}
public void setPersonManager(PersonManager personManager) {
this.personManager = personManager;
}
@Override
public String execute() throws Exception {
String flag = "";
Person per = new Person();
per.setUname(this.username);
per.setUpwd(this.password);
if(personManager.isLogin(per)){
flag = "success";
}
else{
flag = "fail";
}
return flag;
}
}
11. 配置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-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="org.gjt.mm.mysql.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></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>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/user/vo/Person.hbm.xml</value></list>
</property>
</bean>
<!-- 配置PersonManager.java类文件 -->
<bean id="personManager" class="com.user.dao.PersonManager" abstract="false" lazy-init="default" autowire="default" dependency-check="default">
<property name="sessionFactory" >
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置LoginAction.java类文件 -->
<bean id="loginAction" class="com.user.action.LoginAction" >
<property name="personManager">
<ref bean="personManager" />
</property>
</bean>
</beans>
12. 前台页面
login.jsp
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<s:form action="login" method="POST" namespace="/">
<s:textfield name="username" label="用户名" size="14"/>
<s:password name="password" label="密 码" size="14"/>
<s:submit label="提交" />
</s:form>
</center>
</body>
</html>
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<s:form action="login" method="POST" namespace="/">
<s:textfield name="username" label="用户名" size="14"/>
<s:password name="password" label="密 码" size="14"/>
<s:submit label="提交" />
</s:form>
</center>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%
out.println("输入用误,输入的信息为" + "</br>");
out.println("用户名: " + request.getParameter("username") + "</br>");
out.println("密 码: " + request.getParameter("password"));
%>
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%
out.println("输入用误,输入的信息为" + "</br>");
out.println("用户名: " + request.getParameter("username") + "</br>");
out.println("密 码: " + request.getParameter("password"));
%>
result.jsp
<%@ page language="java" contentType="text/HTML;charset=GBK" pageEncoding="GBK"%>
<%
out.println("用户名是: " + request.getParameter("username") + "</br>");
out.println("密 码是: " + request.getParameter("password"));