在juqery的ajax和struts2整合开发时,对象数据的从页面到Action的传递问题

1.首先是最基本的通过表单的提交传值:在struct2 开发时,页面上表单的中输入框的名字,对应与表单要提交的action中的属性名字,当表单提交后,为了更好的说明问题,在此,我将struts中的一个action映射复制过来,<action name="message" class="com.hwadee.action.EchoAction" method="echo">  <result name="success">/view.jsp</result>  </action>,stucts2自动调用action映射的类,然后调用类默认的构造方法,新建一个该类对象,然后调用对象的echo()方法(入伙上面的method=“echo”  没写,则对象默认调用对象的excute()方法。)。此时该对象的属性中的值就为页面表单提交过来的值对应一样的了。

2.然后是通过jquery ajax传值到action中:

<form id="myForm">
               名称:<input type="text" id="username" name="username"/><br/>
               密码:<input type="password" id="password" name="password"/><br/>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <input id="Button1" type="button" class="btn" value="登录"/>
               &nbsp;
               <input id="Button2" type="reset" class="btn" value="取消"/>
         </form>

这是表单的代码。

<script type="text/javascript">
       $(function()
       {
           $("#Button1").click(function(){
              var mName=encodeURI($("#username").val());
              var mPass=encodeURI($("#password").val());
              $.ajax(
              {
                  url:"loginUser.action",
                  dataType:"html",
                  data
:{

username:mName,//前面的username必须跟action中的私有属性username一致。

password:mPass,//前面的password必须跟action中私有属性password一致。

}
                  success:function(strValue
)//strValue是action返回的数据,是文本格式的。
                  {
                     alert(strValue);
                    
                     if(strValue=="yes"){
                         $("#divTip").html("操作提示,登录成功!");
                     }else
                     {
                         $("#divTip").html("用户名或密码错误!");
                     }
                  }
              })
           })
       })
  

以上的js代码。


package struts2.action;
import org.apache.struts2.ServletActionContext;
import com.hwadee.entity.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport {
    private String username;
    private String password;
   



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 execute() throws Exception {

       String message="";
   
       if("wll".equals(username)&& "123".equals(password))
       {
      message="yes";
       }else{
      message="no";
       }
       //向客户端传递数据
    ServletActionContext.getResponse().getWriter().print(message);
       return null;
    }
}


以上是action中的代码。

此时当点击button1按钮时,表单里的数据,就会通过jquery  ajax 异步传送到action中。然后调用excute()方法,返回一个message。然后页面判断message是yes还是no。并给出对应的相应。

3.以上只是传递一个属性,该属性是String类型的。但是,strut2 中,一般用实体类对象来暂时保存数据,也是用实体类对象来当参数来传递数据的。所以在action中就会有很多属性 是实体类的对象。此时,我们用上面方法2的措施是行不通的。就需要下面的方式。

代码基本差不多。

实体类UserInfo

package com.hwadee.entity;


public class UserInfo {
private String username;
private String password;
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 toString(){
return username+"   "+password;
}
}

代码很简单,不用解释。


以下是action类

package struts2.action;
import org.apache.struts2.ServletActionContext;
import com.hwadee.entity.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private UserInfo userinfo;//实体对象。

public UserInfo getUserinfo() {
return userinfo;
}


public void setUserinfo(UserInfo userinfo) {
this.userinfo = userinfo;
}


public String execute() throws Exception {

       String message="";
     System.out.print(userinfo);
       if("wll".equals(userinfo.getUsername())&& "123".equals(userinfo.getPassword()))
       {
      message="yes";
       }else{
      message="no";
       }
       //向客户端传递数据
    ServletActionContext.getResponse().getWriter().print(message);
       return null;
    }
}



以下是html代码:

 <form id="myForm">
               名称:<input type="text" id="userinfo.username" name="userinfo.username"/><br/>     //表单文本框名字对应action实体对象的属性名字。
               密码:<input type="password" id="userinfo.password" name="userinfo.password"/><br/>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <input id="Button1" type="button" class="btn" value="登录"/>
               &nbsp;
               <input id="Button2" type="reset" class="btn" value="取消"/>
            </form>


以下死js代码:

<script type="text/javascript">
       $(function()
       {
           $("#Button1").click(function(){
            //  var mName=encodeURI($("#userinfo.username").val());
             // var mPass=encodeURI($("#userinfo.password").val());
              $.ajax(
              {
                  url:"loginUser.action",
                  dataType:"html",
                  data:$("#myForm").serialize(),//序列化,将表单中的所有数据发送到服务器
                  success:function(strValue)
                  {
                     alert(strValue);
                    
                     if(strValue=="yes"){
                         $("#divTip").html("操作提示,登录成功!");
                     }else
                     {
                         $("#divTip").html("用户名或密码错误!");
                     }
                  }
              })
           })
       })
   
    </script>

此时action中的userinfo对象的username和password属性就和页面文本框中的对应的userinfo.username和userinfo.password的值一样了。



附:struts2.xml

<package name="struts2.action" extends="struts-default">
    <action name="loginUser" class="struts2.action.LoginAction">
    </action>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值