extjs4 表单提交到struts2 action 的响应问题

来源:互联网 发布:足球和篮球知乎 编辑:程序博客网 时间:2024/05/16 06:06

extjs4 表单提交到struts2 action 的响应问题

 (2012-07-20 13:25:49)
转载
标签: 

extjs4

 

表单提交到

 

action

 

的响应问题

 

杂谈

 分类: extjs

1、我用extjs4编写一个登陆的表单,提交到struts2的 action 后成功后,浏览器接收到的响应不但有我自己编写的success ,还有一堆html 原文件代码,应为默认下,extjs4 form收到{sucess:XX}格式的json才会执行form.submit()中的success 和failure

2、我的extjs4表单代码

 Ext .require(['Ext.window.MessageBox.*', 'Ext.tip.*', 'Ext.form.*',
    'Ext.Ajax.*']);

Ext.onReady(function() {
 Ext.createWidget('form', {
  renderTo : 'login',
  title : 'welcom to login BBS',
  bodyPadding : 5,
  frame : true,
  width : 340,
  id : 'loginform',
    url: 'loginAction.action',
  defaultType : 'textfield',
  fieldDefaults : {
   labelAlign : 'left',
   labelWidth : 105,
   anchor : '100%'
  },
  items : [{

     fieldLabel : 'userName',
     name : 'userName',
     id : 'userName',
     allowBlank : false,
     emptyText : 'please enter the userName'
    }, {

     fieldLabel : 'userPassword',
     name : 'userPassword',
     id : 'userPassword',
     allowBlank : false,
     emptyText : 'please enter the userPassword'

    }],
  buttons : [{
     text : 'Reset',
     handler : function() {
      this.up('form').getForm().reset();
     }
    }, {
     text : 'Submit',
     formBind : true, // only enabled once the form is valid
     disabled : true,
     handler : function() {
      var form = this.up('form').getForm();
      if (form.isValid()) {
       form.submit({
        success : function(form, action) {
         
         Ext.Msg.alert('Success', action.result.msg);
         alert('Success');
        },
        failure : function(form, action) {
         Ext.Msg.alert('Failed', action.result.msg);
         alert('Failed');
        }
       });
      }
     }
    }]

 });
});

extjs4 <wbr>表单提交到struts2 <wbr>action <wbr>的响应问题

3、action 代码

 package com.bbs.action.user;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.bbs.dao.user.UserExistsDAO;
import com.bbs.model.TbUser;
import com.bbs.util.MD5;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component
@Scope("prototype")
public class LoginAction extends ActionSupport  {

 
 private static final long serialVersionUID = 1L;
   private String userName;
  
   private String userPassword;
 

 private TbUser user; //

 
 private UserExistsDAO userExist;// 用于检测用户是否已经存在
 
  
   public String getUserName() {
 return userName;
}


public void setUserName(String userName) {
 this.userName = userName;
}


public String getUserPassword() {
 return userPassword;
}


public void setUserPassword(String userPassword) {
 this.userPassword = userPassword;
}

 

 @Override
 public String execute() {
  user = new TbUser();
           user.setUserName(userName);
            user.setUserPassword(MD5.messgeDigest(userPassword));
  
  String msg = "";//用于暂时保存返回信息
  ActionContext context = ActionContext.getContext();// 获取action上下文

  // HttpServletRequest request = (HttpServletRequest)
  // context.get(ServletActionContext.HTTP_REQUEST);
  // Map session = context.getSession(); //获取sesion

  HttpServletResponse response = (HttpServletResponse) context
    .get(ServletActionContext.HTTP_RESPONSE);

  if (userExist.loginCheck(user)) {
   msg = "{success:true}";// 用户验证成功
   try {
    
    response.getWriter().write(msg);
    
    System.out.println(user.getUserName()+" "+user.getUserPassword());
    
   } catch (IOException e) {

    e.printStackTrace();
   }

   return SUCCESS;
  }
  
  msg = "{success:false,errors:{userPassword:'the password is not sure'}}";
  try {
   response.getWriter().write(msg);
  } catch (IOException e) {

   e.printStackTrace();
  }
  
  return INPUT;

 }
   public TbUser getUser() {
  return user;
 }

 public void setUser(TbUser user) {
  this.user = user;
 }

 public static long getSerialversionuid() {
  return serialVersionUID;
 }

 public UserExistsDAO getUserExist() {
  return userExist;
 }

 @Autowired
 @Qualifier("userExistsDAOImp")
 public void setUserExist(UserExistsDAO userExist) {
  this.userExist = userExist;
 }

 
}

 

4、struts.xml的配置

 <!-- 登录action -->
  <action name="loginAction" class="com.bbs.action.user.LoginAction">
    <result type="json"/> 
   </action>

 

5、浏览器返回的响应

   a、输入正确用户和密码时

 extjs4 <wbr>表单提交到struts2 <wbr>action <wbr>的响应问题

   b、输入不正确用户和密码时

  extjs4 <wbr>表单提交到struts2 <wbr>action <wbr>的响应问题

解决办法

   1、表单提交到action 后,如果在struts.xml中配置action 的返回类型是json ,struts2就会把action 中的所有getXXX方法封装成{XXX:‘’}的json格式。我现在不需要封装成json的get方法上增加 @JSON(serialize = false),告诉struts2不要自作主张把我的get 方法封装,因为extjs4 表单的返回必须是{success:}的形式,所我在action 中增加sucess 属性,让struts2自动帮我封装成{success:}的形式

  2、改进后的extjs form

 Ext
  .require(['Ext.window.MessageBox.*', 'Ext.tip.*', 'Ext.form.*',
    'Ext.Ajax.*']);

Ext.onReady(function() {
 Ext.createWidget('form', {
  renderTo : 'login',
  title : 'welcom to login BBS',
  bodyPadding : 5,
  frame : true,
  width : 340,
  id : 'loginform',
    url: 'loginAction.action',
  defaultType : 'textfield',
  fieldDefaults : {
   labelAlign : 'left',
   labelWidth : 105,
   anchor : '100%'
  },
  items : [{

     fieldLabel : 'userName',
     name : 'userName',
     id : 'userName',
     allowBlank : false,
     emptyText : 'please enter the userName'
    }, {

     fieldLabel : 'userPassword',
     name : 'userPassword',
     id : 'userPassword',
     allowBlank : false,
     emptyText : 'please enter the userPassword'

    }],
  buttons : [{
     text : 'Reset',
     handler : function() {
      this.up('form').getForm().reset();
     }
    }, {
     text : 'Submit',
     formBind : true, // only enabled once the form is valid
     disabled : true,
     handler : function() {
      
      var form = this.up('form').getForm();
      if (form.isValid()) {
       form.submit({
        success : function(form, action) {
         
         //Ext.Msg.alert('Success', action.result.msg);
         alert('Success');
        },
        failure : function(form, action) {
         //Ext.Msg.alert('Failed', action.result.msg);
         alert(action.result.msg+"errors");
        }
       });
      }
     }
    }]

 });
});

  3、改进后的action

package com.bbs.action.user;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.bbs.dao.user.UserExistsDAO;
import com.bbs.model.TbUser;
import com.bbs.util.MD5;
import com.opensymphony.xwork2.ActionSupport;

@Component
@Scope("prototype")
public class LoginAction extends ActionSupport {

 
 private static final long serialVersionUID = 1L;
 private String userName;
 private String userPassword;
 private TbUser user; //
 private UserExistsDAO userExist;// 用于检测用户是否已经存在
 private boolean success=false;//记录登录是否成功

 @JSON(serialize = false)
 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 @JSON(serialize = false)
 public String getUserPassword() {
  return userPassword;
 }

 public void setUserPassword(String userPassword) {
  this.userPassword = userPassword;
 }

 @Override
 public String execute() {
  user = new TbUser();
  user.setUserName(userName);
  user.setUserPassword(MD5.messgeDigest(userPassword));

  success=userExist.loginCheck(user);
  if (success) {
   
     System.out.println(user.getUserName() + " "
      + user.getUserPassword() + "success");
 

   return SUCCESS;
  }

  
  return INPUT;

 }

 @JSON(serialize = false)
 public TbUser getUser() {
  return user;
 }

 public void setUser(TbUser user) {
  this.user = user;
 }

 @JSON(serialize = false)
 public static long getSerialversionuid() {
  return serialVersionUID;
 }
 @JSON(serialize = false)
 public UserExistsDAO getUserExist() {
  return userExist;
 }

 @Autowired
 @Qualifier("userExistsDAOImp")
 public void setUserExist(UserExistsDAO userExist) {
  this.userExist = userExist;
 }
 @JSON(serialize = true)
 public boolean isSuccess() {
  return success;
 }

 public void setSuccess(boolean success) {
  this.success = success;
 }

}

  4、改进后的struts.xml

<!-- 登录action -->
  <action name="loginAction" class="com.bbs.action.user.LoginAction">
     <result name="success" type="json"/>
     <result name="input" type="json"/>
      
        </action>

5、在form 中输入正确的用户名和密码后浏览器响应

extjs4 <wbr>表单提交到struts2 <wbr>action <wbr>的响应问题

6、在form 中输入不正确的用户名和密码后浏览器响应
extjs4 <wbr>表单提交到struts2 <wbr>action <wbr>的响应问题

0 0