Struts2整合jQuery实现Ajax功能-IE与firefox的区别

action:

View Code
 1 package com.practice.action;
 2 
 3 import javax.servlet.http.HttpServletResponse;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 import com.opensymphony.xwork2.ModelDriven;
 9 import com.practice.entity.User;
10 
11 public class UserAction extends ActionSupport implements ModelDriven<User>{
12     
13     private User user = new User();
14     private String loginInfo;
15     
16     public String getLoginInfo() {
17         return loginInfo;
18     }
19     
20     public void setLoginInfo(String loginInfo) {
21         this.loginInfo = loginInfo;
22     }
23     public String login() throws Exception{
24         if(user.getId() == 1 && user.getPwd().equals("aa")) {
25             loginInfo = "logintrue";
26         }else {
27             loginInfo = "loginfalse";
28         }
29         HttpServletResponse response = ServletActionContext.getResponse();
30         response.getWriter().write(loginInfo);
31         return SUCCESS;
32     }
33 
34     public User getModel() {
35         return user;
36     }
37     
38     public void test() throws Exception{
39         if(user.getId() == 1 && user.getPwd().equals("aa")) {
40             loginInfo = "logintrue";
41         }else {
42             loginInfo = "loginfalse";
43         }
44         HttpServletResponse response = ServletActionContext.getResponse();
45         response.getWriter().write(loginInfo);
46     }
47 }

entity:

View Code
 1 package com.practice.entity;
 2 
 3 public class User {
 4     private int id;
 5     
 6     public int getId() {
 7         return id;
 8     }
 9     public void setId(int id) {
10         this.id = id;
11     }
12     private String pwd;
13     
14     public String getPwd() {
15         return pwd;
16     }
17     public void setPwd(String pwd) {
18         this.pwd = pwd;
19     }
20     
21 
22 }

index.jsp

View Code
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     <title>practice</title>
12   </head>
13   <script type="text/javascript" src="script/jquery/jquery-1.6.min.js"></script>
14   
15   <body>
16   
17   <form id="loginForm" name="loginForm">
18        <input type="text" id="uId" name="id"/>
19        <input type="password" id="pwd" name="pwd"/>
20        <input type="button" id="loginBut" value="登录"/>
21   </form>
22   
23   <div id="Info"></div>
24   </body>
25   <script type="text/javascript" language="javascript">
26      $("#loginBut").click(function() {
27           var $uId = $("#uId");
28           var $pwd = $("#pwd");
29       
30       if($uId.val() == "") {
31           alert("请输入用户ID");
32           $uId.focus();
33           return false;
34       }
35       
36       if($pwd.val() == "") {
37           alert("请输入密码");
38           $pwd.focus();
39           return false;
40       }
41       
42       else {
43           var parameter = {
44             url : "login.action",
45             type : "POST",
46             data : $("#loginForm").serialize(),
47             //data : {"id":$('#uId').val(), "pwd":$('#pwd').val()},
48             success : function(loginInfo) {
49                 alert("returnInfo:" + loginInfo);
50                 if(loginInfo == "logintrue") {
51                     alert("登录成功");
52                 }else {
53                     alert("登录失败");
54                     }
55             }
56           };
57           //$('#Info').html("正在登录中。。。");
58           $.ajax(parameter);
59       }
60   });
61   </script>
62 </html>

error.jsp

View Code
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%@taglib prefix="s" uri="/struts-tags"%>
 3 <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11   <head>
12     <base href="<%=basePath%>">
13     <title>ERROR</title>
14   </head>
15 
16   <body>
17 <c:out value="loginInfo"></c:out>
18   </body>
19 </html>

success.jsp

View Code
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     <title>SUCCESS</title>
13   </head>
14 
15   <body>
16 <c:out value="loginInfo"></c:out>
17   </body>
18 </html>

struts.xml

View Code
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4  <constant name="struts.devMode" value="true"/> 
 5  <constant name="struts.configuration.xml.reload" value="true"/>
 6   <package name="practice" namespace="/" extends="struts-default">
 7        
 8        
 9        
10            <action name="login" class="com.practice.action.UserAction" method="test">
11            </action>
12        <!--  
13        <action name="login" class="com.practice.action.UserAction" method="login">
14           <result name="success">/success.jsp</result>
15           <result name="error">/error.jsp</result>
16        </action>
17        -->
18   </package>
19 </struts>    

当选择没有返回值的test的方法测试:

在IE下,返回值可以得到,

在firefox下,却得不到,得到的是

然而,用有返回值的login方法测试时:

都是一样的返回的都是

结论 :

IE:当调用action的方法有返回值时,response.getWriter().write(loginInfo)是无效的,服务器都会返回 对应的返回值所对应的视图;反之,则有效,返回loginInfo。

firefox:调用的action的方法有返回值,response.getWriter().write(loginInfo)是无效的,服务器都会返回 对应的返回值所对应的视图;反之,也接收不到loginInfo。

posted on 2012-05-19 22:23  lovebeauty  阅读(523)  评论(0编辑  收藏  举报

导航