`
zhengdl126
  • 浏览: 2542654 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

Spring 基础和入门:Struts2+Spring整合

    博客分类:
  • java
阅读更多

http://www.tudou.com/programs/view/dc_75kAOWVY/  spring入门视频教程1  实例可学

 

http://v.youku.com/v_playlist/f3278836o1p2.html
http://v.youku.com/v_playlist/f803046o1p2.html
轻量级



7个模块:
AOP
DAO





loc 控制反转

aop 面向切面编程 面向方面


对比EJB

交给spring管理session,事务,对象之间的装配,依赖关系,对象之

间的创建




java project





-----------------------------------------------Struts2+Spring整合  附 Struts2+Spring整合TestSS.rar 下载


 

 

 

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">
 
     <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>index.jsp</welcome-file>
    </welcome-file-list>
  
   
    <!-- Spring提供ServletContextListener的一个实现类ContextLoaderListener,
    该类可以作为listener 使用,它会在创建时候自动查找WEB-INF/下
    的applicationContext.xml文件,因此,如果只有一个配置文件,并且文件名为
     applicationContext.xml,只需在web.xml文件中增加如下一段即可: -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
   
   
     <!-- 如果有多个配置文件需要载入,则考虑使用<context-param>元素来确定配
    置文件的文件名。 ContextLoaderListener加载时,会查找名为contextConfigLocation
    的参数。因此,配置context- param时参数名字应该是contextConfigLocation。 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>
   
   
</web-app>

 

 

 

 

 

 

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.xml中进行配置,处理页面提交的请求,配置action:login,login
 一定要和login.jsp中form的action属性名匹配。没有指定具体的 包.class -->
     <package name="struts2" extends="struts-default">
         <action name="login" class="loginAction">
             <result name="success">welcome.jsp</result>
            <result name="input">login.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>
</struts>

 

 

 

 

 

 

applicationContext-common.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:tx="http://www.springframework.org/schema/tx"
     xmlns:aop="http://www.springframework.org/schema/aop"
     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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

</beans>

 

 

applicationContext-beans.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:tx="http://www.springframework.org/schema/tx"
     xmlns:aop="http://www.springframework.org/schema/aop"
     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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- 实现类配置,以让Spring进行管理 -->
    <bean id="loginManager"
        class="com.test.manager.impl.LoginManagerImpl">
    </bean>
   
   
</beans>

 

 

 

applicationContext-actions.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:tx="http://www.springframework.org/schema/tx"
     xmlns:aop="http://www.springframework.org/schema/aop"
     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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- Spring来管理LoginAction -->
    <bean id="loginAction" class="com.test.action.LoginAction"
        scope="prototype">
        <property name="loginManager" ref="loginManager"></property>
    </bean>
</beans>

 

 

LoginAction.java

 

package com.test.action;
/*
 * 新建LoginAction.java,继承ActionSupport类
包含从页面所接收参数username、password,以及业务逻辑处理类
LoginManager类型的loginManager,给username和password设置
get、set,给loginManager设置set方法,以让Spring为我们自动注入;
overwrite父类中的

 * */
import com.opensymphony.xwork2.ActionSupport;
import com.test.manager.LoginManager;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
    private LoginManager loginManager;
    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 void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }
   
    @Override
    public String execute() throws Exception {

        if(loginManager.isLogin(username, password))
        {
            return SUCCESS;
        }
        return INPUT;
    }
}

 

 

LoginManager.java

package com.test.manager;
//业务逻辑处理的接口
public interface LoginManager {
        public boolean isLogin(String username, String password);
    }

 

 

 

LoginManagerImpl.java

 

package com.test.manager.impl;
//业务逻辑处理的实现类
import com.test.manager.LoginManager;

public class LoginManagerImpl implements LoginManager{
    public boolean isLogin(String username, String password)
    {
        if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))
        {
            return true;
        }
        return false;
    }
}

 

 

login.jsp

 

<%@ page language="java" contentType="text/html; charset=GB18030"
     pageEncoding="GB18030"%>
 <%@ taglib prefix="s" uri="/struts-tags"%>
 <!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=GB18030">
         <title>登录页面</title>
     </head>
    <body>
        <s:form action="login" method="post">
            <s:textfield name="username" label="username" />
            <s:password name="password" label="password" />
            <s:submit value="submit" />
        </s:form>
    </body>
</html>

 

 

welcome.jsp

 

 

<%@ page language="java" contentType="text/html; charset=GB18030"
     pageEncoding="GB18030"%>
 <!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=GB18030">
         <title>登录成功</title>
     </head>
     <body>
        用户名:${username}
        <br>
        密码:${password}
        <br>
    </body>
</html>

 

 

error.jsp

 

 

 

  • 大小: 41.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics