Spring 整合struts2

6 篇文章 0 订阅
0 篇文章 0 订阅

开发工具Eclipse Java EE IDE for Web Developers.数据库mysql.

struts-2.3.14,spring-framework-3.2.2.RELEASE,tomcat7


一、配置struts2

1、导入struts2的jar包

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

commons-lang3-3.1.jar

commons-logging-1.1.1.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.6.jar

struts2-core-2.3.14.jar

xwork-core-2.3.14.jar


2、修改web.xml,添加以下内容

<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>
3、创建action

package action;

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{
	
	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 String exeute(){
		if("hello".equals(userName)&&"123".equals(passWord)){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

}
4、创建error.jsp和success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆失败
</body>
</html>

5、在src下配置struts.xml

<!DOCTYPE struts PUBLIC  
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
      "http://struts.apache.org/dtds/struts-2.0.dtd"> 
          
<struts>
    <package name="/" extends="struts-default">
        <action name="login" class="action.LoginAction" method="exeute">
          <result name="success">/success.jsp</result>
          <result name="error">/error.jsp</result>
        </action>
    </package>

    
</struts>
6、创建登陆页面index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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 HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My JSP 'index.jsp' starting page</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">  
</head>
<body >
 <s:form action="login">
   <s:textfield name="userName" label="登录名"></s:textfield>
   <s:password name="passWord" label="密码"></s:password>
   <s:submit value="登陆" key="login"></s:submit>
 
 </s:form>
</body>
</html>
启动tomcat测试


二、配置spring

1、导入spring的jar

 

导入struts2-spring-plugin-2.3.14.jar

2、配置web.xml,加入spring配置

<!--初始化Sprig  -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置Spring监听器 -->
  <listener>    
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
   </listener> 
3、创建dao和实现类

package dao;

public interface UserDao {
	public boolean login(String userName,String passWord);  

}
package dao.impl;


import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import dao.UserDao;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao{

	@Override
	public boolean login(String userName, String passWord) {
		// TODO Auto-generated method stub
		String sql="select * from user where username='"+userName+"' and pwd='"+passWord+"'";  
        List map=this.getJdbcTemplate().queryForList(sql);
        try{  
            if(map.size()!=0){            
                return true;  
            }else{  
                return false;  
            }  
        }catch(EmptyResultDataAccessException e){  
            e.printStackTrace();  
            return false;  
        }         
	}

	
}

LoginAction修改如下:

package action;

import com.opensymphony.xwork2.ActionSupport;

import dao.UserDao;

public class LoginAction extends ActionSupport{
	
	private UserDao  userdao;
	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 UserDao getUserdao() {
		return userdao;
	}
	public void setUserdao(UserDao userdao) {
		this.userdao = userdao;
	}
	
	public String exeute(){
		if(userdao.login(userName, passWord)){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

}



4、在src目录下创建spring配置文件applicationContext.xml和jdbc.properties

jdbc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lol
jdbc.username=root
jdbc.password=123

applicationContext.xml: 加入c3p0-0.9.1.jar

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
					http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
					http://www.springframework.org/schema/aop 
					http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
					http://www.springframework.org/schema/tx 
					http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
					http://www.springframework.org/schema/context 
					http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <!--映入src下的jdbc.porpertise 文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
    
	
   <!-- 配置数据源 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"   destroy-method="close">
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		 <property name="minPoolSize"><value>1</value></property>
         <property name="maxPoolSize"><value>20</value></property>
         <property name="maxIdleTime"><value>1800</value></property>
         <property name="acquireIncrement"><value>2</value></property>
         <property name="maxStatements"><value>0</value></property>
         <property name="initialPoolSize"><value>2</value></property>
         <property name="idleConnectionTestPeriod"><value>1800</value></property>
         <property name="acquireRetryAttempts"><value>30</value></property>
         <property name="breakAfterAcquireFailure"><value>true</value></property>
         <property name="testConnectionOnCheckout"><value>false</value></property>
	</bean>		
	<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource">  
            <ref local="dataSource" />  
        </property>  
    </bean>  
	<bean id="txManager" abstract="true"  
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
        <property name="transactionManager" ref="transactionManager" />  
        <property name="transactionAttributes">  
            <props>  
                <prop key="add*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="save*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="edit*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="update*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="del*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="handle*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="notify*">  
                    PROPAGATION_REQUIRED,-DataAccessException  
                </prop>  
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="query*">PROPAGATION_REQUIRED,readOnly,-DataAccessException</prop>  
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>  
            </props>  
        </property>  
    </bean>  
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <constructor-arg ref="dataSource" />  
    </bean>  				
    <bean id="userDao" class="dao.impl.UserDaoImpl">    
          <property name="jdbcTemplate" ref="jdbcTemplate"></property>  
    </bean>   
    <bean id="login" class="action.LoginAction">    
        <property name="userdao" ref="userDao" />    
    </bean>  

</beans>

修改strut.xml如下:

<!DOCTYPE struts PUBLIC  
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
      "http://struts.apache.org/dtds/struts-2.0.dtd"> 
          
<struts>
    <package name="/" extends="struts-default">
        <action name="login" class="login" method="exeute">
          <result name="success">/success.jsp</result>
          <result name="error">/error.jsp</result>
        </action>
    </package>

    
</struts>


加入mysql包:mysql-connector-java-5.1.17.jar

启动tomcat测试。OK




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值