Spring提供了DelegatingActionProxy类用来代替struts中的动作负责在Spring配置文档中查找对应的动作映射从而把Struts的Action与Spring分离并把struts的动作置于Spring框架的控制下
新建一个登陆页面loginDelegatingActionProxyjsp示例代码如下
<%@page contentType=text/html;charset=GBK isELIgnored=false%>
<html>
<head>
<title>实现用户登录的Struts实例</title>
</head>
<body>
<font size=> ${msg}<br></font>
<form name=form action=/myLogin/loginDelegatingActionProxydo
method=post>
用户名
<input type=text name=username value=${userusername}/>
<br>
密码
<input type=password name=password value=${userpassword}/>
<br>
<input type=submit name=method value=提交 />
</form>
</body>
</html>
创建一个存储登陆用户信息的类Userjava该类继承于ActionForm代码如下
package comzhaosoftbean;
import orgapachestrutsactionActionForm;
publicclass User extends ActionForm {
private String username=null;
private String password=null;
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
thisusername = username;
}
public String getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
thispassword = password;
}
}
在 Comzhaosoftaction中新建一个控制器LoginDelegatingActionProxyjava该类继承于Struts的Action代码如下
package comzhaosoftaction;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import comzhaosoftbeanUser;
import comzhaosoftdomainLogin;
publicclass LoginDelegatingActionProxy extends Action {
private Login login;
public Login getLogin() {
returnlogin;
}
publicvoid setLogin(Login login) {
thislogin = login;
}
public ActionForward execute(ActionMapping mapping ActionForm form
HttpServletRequest request HttpServletResponse response)
throws Exception {
String forwardJsp = login;
getLogin()login((User) form);
requestsetAttribute(msg logingetMsg());
requestsetAttribute(user (User) form);
return mappingfindForward(login);
}
}
新建包comzhaosoftdomain和comzhaosoftdomainimpl在包comzhaosoftdomain中新建一个接口Loginjava用来定义登陆的逻辑处理示例代码如下
package comzhaosoftdomain;
import comzhaosoftbeanUser;
publicinterface Login {
publicabstractvoid login(User user);
publicabstract String getMsg();
}
. 在包comzhaosoftdomainimpl中建立LoginImpljava用来实现登陆的逻辑处理:
package comzhaosoftdomainimpl;
import comzhaosoftbeanUser;
import comzhaosoftdomainLogin;
publicclass LoginImpl implements Login {
private String msg;
public String getMsg() {
returnthismsg;
}
publicvoid login(User user) {
String username = usergetUsername();
String password = usergetPassword();
if (username == null && password == null) {
msg = 请输入用户名和密码;
} elseif (equals(username) || equals(password)) {
msg = 请输入用户名和密码;
} elseif (zhaosoftequals(username) && zhaosoftequals(password)) {
msg = 登陆成功;
} elseif (!zhaosoftequals(username)) {
msg = 用户名填写错误;
}
}
}
修改struts的配置文件strutsconfigxml注册ContextLoaderPlugIn插件实例代码如下
<?xml version= encoding=UTF?>
<!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>
<strutsconfig>
<!定义formbean>
<formbeans>
<formbean name=helloWorld
type=comzhaosoftbeanHelloWorld />
<formbean name=user type=comzhaosoftbeanUser></formbean>
</formbeans>
<actionmappings>
<action path=/loginDelegatingActionProxy
type=orgspringframeworkwebstrutsDelegatingActionProxy
name=user>
<forward name=login
path=/jsp/loginDelegatingActionProxyjsp>
</forward>
</action>
<action path=/loginDelegatingActionProxyTemp
type=orgapachestrutsactionsForwardAction
parameter=/jsp/loginDelegatingActionProxyjsp>
</action>
</actionmappings>
<plugin
className=orgspringframeworkwebstrutsContextLoaderPlugIn>
<setproperty property=contextConfigLocation
value=/WEBINF/springconfigxml />
</plugin>
</strutsconfig>
编写Spring的配置文件springconfigxml放在WEBINF目录中代码如下
<?xml version= encoding=UTF?>
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
<bean id=login class=comzhaosoftdomainimplLoginImpl>
</bean>
<! 注意和struts路径的对应 >
<bean name=/loginDelegatingActionProxy class=comzhaosoftactionLoginDelegatingActionProxy>
<property name=login>
<ref bean=login/>
</property>
</bean>
</beans>