论坛首页 Java企业应用论坛

Struts整合spring方法(二)

浏览 2188 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-01  
用Spring的DelegatingRequestProcessor类来代替struts中的RequestProcessor类,从而把struts中的action与Spring分离。并把struts的动作置于Spring框架的控制下。
1。新建一个登陆页面:loginDelegatingRequestProcessor.jsp
<%@page contentType="text/html;charset=GBK" isELIgnored="false"%>
<html>
<head><title>实现用户登录的Struts实例</title></head>
<body>
    <font size=’22’>${msg}<br></font>
    <form name="form1" action="/myLogin2/loginDelegatingRequestProcessor.do" method="post">
        用户名:<input type="text" name="username" value="${user.username}"/><br>
       密码:<input type="password" name="password" value="${user.password}"/><br>
        <input type="submit" name=”method” value="提交"/>
    </form>
</body>
</html>
 
2。创建一个存储登陆用户信息的类:User.java该类继承于ActionForm,代码如下:
package com.zhaosoft.bean;
 
import org.apache.struts.action.ActionForm;
 
publicclass User extends ActionForm {
 
    private String username=null;
    private String password=null;
    public String getUsername() {
       returnusername;
    }
    publicvoid setUsername(String username) {
       this.username = username;
    }
    public String getPassword() {
       returnpassword;
    }
    publicvoid setPassword(String password) {
       this.password = password;
    }
    
    
}
 
3 Com.zhaosoft.action中新建一个LoginDelegationRequestProcessor.java,该类继承于strutsAction,代码示例如下:
package com.zhaosoft.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
import com.zhaosoft.bean.User;
import com.zhaosoft.domain.Login;
 
public class LoginDelegationRequestProcessor extends Action {
 
       private Login login;
 
       public Login getLogin() {
              return login;
       }
 
       public void setLogin(Login login) {
              this.login = login;
       }
 
       public ActionForward execute(ActionMapping mapping, ActionForm form,
                     HttpServletRequest request, HttpServletResponse response)
                     throws Exception {
              String forwardJsp = "login";
              getLogin().login((User) form);
              request.setAttribute("msg", login.getMsg());
              request.setAttribute("user", (User) form);
 
              return mapping.findForward("login");
       }
 
}
 
4。 新建包:com.zhaosoft.domaincom.zhaosoft.domain.impl在包com.zhaosoft.domain中新建一个接口:Login.java用来定义登陆的逻辑处理,示例代码如下:
package com.zhaosoft.domain;
 
import com.zhaosoft.bean.User;
 
publicinterface Login {
publicabstractvoid login(User user);
publicabstract String getMsg();
}
5. 在包com.zhaosoft.domain.impl中建立LoginImpl.java用来实现登陆的逻辑处理
package com.zhaosoft.domain.impl;
 
import com.zhaosoft.bean.User;
import com.zhaosoft.domain.Login;
 
publicclass LoginImpl implements Login {
 
    private String msg;
    public String getMsg() {
       returnthis.msg;
    }
 
    publicvoid login(User user) {
       String username = user.getUsername();
       String password = user.getPassword();
       if (username == null && password == null) {
           msg = "请输入用户名和密码";
       } elseif ("".equals(username) || "".equals(password)) {
           msg = "请输入用户名和密码";
       } elseif ("zhaosoft".equals(username) && "zhaosoft".equals(password)) {
           msg = "登陆成功";
       } elseif (!"zhaosoft".equals(username)) {
           msg = "用户名填写错误";
       }
    }
 
}
 6.修改struts的配置文件struts-config.xml,注册ContextLoaderPlugIn插件,实例代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
    <!--定义formbean-->
    <form-beans>
       <form-bean name="user" type="com.zhaosoft.bean.User"></form-bean>
    </form-beans>
    <action-mappings>
       <action path="/loginDelegatingRequestProcessor"
           type="com.zhaosoft.action.LoginDelegationRequestProcessor"
           name="user">
           <forward name="login"
              path="/jsp/loginDelegatingRequestProcessor.jsp">
           </forward>
       </action>
       <action path="/loginDelegatingRequestProcessorTemp"
           type="org.apache.struts.actions.ForwardAction"
           parameter="/jsp/loginDelegatingRequestProcessor.jsp">
       </action>
    </action-mappings>
    <controller
       processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
    </controller>
    <plug-in
       className="org.springframework.web.struts.ContextLoaderPlugIn">
       <set-property property="contextConfigLocation"
           value="/WEB-INF/spring-config.xml" />
    </plug-in>
</struts-config>
 
7. 编写Spring的配置文件spring-config.xml。放在WEB-INF目录中,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="login" class="com.zhaosoft.domain.impl.LoginImpl">
    </bean>
    <bean name="/loginDelegatingRequestProcessor" class="com.zhaosoft.action.LoginDelegationRequestProcessor">
    <property name="login">
    <ref bean="login"/>
    </property>
    </bean>
</beans>
 
  我喜欢的
浏览器
  我喜欢的
文化礼品
  我喜欢的
网站
FireFox 2.0
100部奥斯卡影片
博告网
   
   
  加入博告网日进斗金不是梦!
聚合到我的好诶网博告网 提供的广告
 
  我喜欢的
浏览器
  我喜欢的
文化礼品
  我喜欢的
ISP网站
  我喜欢的
网站
FireFox 2.0
100部奥斯卡影片
时代互联
博告网
   
   
  时代互联10元换空间
  加入博告网日进斗金不是梦!
聚合到我的好诶网博告网 提供的广告
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics