《struts2权威指南》学习笔记之struts2整合Spring

整合Spring,换句话说,也就是让spring的IOC功能为我们的struts action注入逻辑组件

首先需要加载struts2-spring-plugin-2.0.6.jar这个包,这个是关键,他可以帮我们把struts2和spring仅仅整合在一起

 

首先是web.xml

 

<? xml version="1.0" encoding="utf-8" ?>
< web-app  version ="2.4"  xmlns ="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    
< context-param >
      
< param-name > contextConfigLocation </ param-name >
      
< param-value > /WEB-INF/bean.xml </ param-value >
    
</ context-param >

    
< listener >
      
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
    
</ listener >
    
< 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 >

    
< filter >
        
< filter-name > aaa </ filter-name >
        
< filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp  </ filter-class >
    
</ filter >


    
< filter-mapping >
        
< filter-name > aaa </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >


</ web-app >

 

其次是struts2.xml

 

<? xml version="1.0" encoding="GBK" ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >

    
< package  name ="lee"   extends ="struts-default" >
      
< action  name ="login"  class ="loginAction" >
        
< result  name ="error" > /error.jsp </ result >
        
< result  name ="success" > /welcome.jsp </ result >
      
</ action >
    
</ package >

</ struts >

 

最关键的是bean.xml,完成为struts2 action的注入

 

<? xml version="1.0" encoding="GBK" ?>
<!--  定义Spring配置文件的根元素,并指定Schema信息  -->
< 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.xsd" >

  
< bean  id ="myService"  class ="lee.MyServiceImpl" />
  
< bean  id ="loginAction"  class ="lee.LoginAction"  scope ="prototype" >
    
< property  name ="ms"  ref ="myService" />
  
</ bean >
</ beans >

 

LoginAction

 

package  lee;

import  com.opensymphony.xwork2.Action;

public   class  LoginAction  implements  Action  {

    
private String username;
    
private String password;
    
private String tip;
    
private MyService ms;
    
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 getTip() {
        
return tip;
    }


    
public void setTip(String tip) {
        
this.tip = tip;
    }


    
public String execute() throws Exception {
        
if(ms.valid(this.getUsername(), this.getPassword())){
            
return SUCCESS;
        }

        
else{
            
return ERROR;
        }

    }


    
public MyService getMs() {
        
return ms;
    }


    
public void setMs(MyService ms) {
        
this.ms = ms;
    }


}

 

MyService及实现

 

package  lee;


public   interface  MyService 
{
    
boolean valid(String username , String pass);
}



package  lee;

public   class  MyServiceImpl  implements  MyService
{
    
public boolean valid(String username , String pass)
    
{   System.out.println(username);
        
if (username.equals(""&& pass.equals("1234") )
        
{
            
return true;
        }

        
return false;
    }

}

 

struts.properties

struts.i18n.encoding=gb2312

 

login.jsp


   <%@ page contentType="text/html; charset=gb2312"%>


<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
    <table align="center">
    <caption><h3>用户登录</h3></caption>
        <tr>
            <td>用户名:<input type="text" name="username"/></td>
        </tr>
        <tr>
            <td>密&nbsp;&nbsp;码:<input type="text" name="password"/></td>
        </tr>
        <tr align="center">
            <td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
        </tr>
    </table>
</form>
</body>
</html>

 

welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>成功页面</title>
    </head>
    <body>
        您已经登录!
        <s:property value="username"/>
  <s:property value="tip"/>
    </body>
</html>

 

error.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
    <head>
        <title>错误页面</title>
    </head>
    <body>
        您不能登录!
    </body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值