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

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

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

首先是web.xml

<? xmlversion="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/j2eehttp://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

<? xmlversion="1.0"encoding="GBK" ?>
<! DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.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的注入

<? xmlversion="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/beanshttp://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 ... {

privateStringusername;
privateStringpassword;
privateStringtip;
privateMyServicems;
publicStringgetUsername()...{
returnusername;
}


publicvoidsetUsername(Stringusername)...{
this.username=username;
}


publicStringgetPassword()...{
returnpassword;
}


publicvoidsetPassword(Stringpassword)...{
this.password=password;
}


publicStringgetTip()...{
returntip;
}


publicvoidsetTip(Stringtip)...{
this.tip=tip;
}


publicStringexecute()throwsException...{
if(ms.valid(this.getUsername(),this.getPassword()))...{
returnSUCCESS;
}

else...{
returnERROR;
}

}


publicMyServicegetMs()...{
returnms;
}


publicvoidsetMs(MyServicems)...{
this.ms=ms;
}


}

MyService及实现

package lee;


public interface MyService
... {
booleanvalid(Stringusername,Stringpass);
}



package lee;

public class MyServiceImpl implements MyService
... {
publicbooleanvalid(Stringusername,Stringpass)
...{System.out.println(username);
if(username.equals("")&&pass.equals("1234"))
...{
returntrue;
}

returnfalse;
}

}

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值