日期:2014-05-17  浏览次数:20790 次

各位大牛帮忙看一下这段代码,冥思苦想就是找不到原因?
html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>login.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="login.js"></script>
</head>

<body>
<p>
<input type="text" name="textfield" id="name"/>
</p>
<p>
<input type="text" name="textfield2" id="password"/>
</p>
<p>
<input type="submit" name="Submit" value="登录" onclick="login();"/>
</p>
</body>
</html>
js文件:
function login(){
//var u = "userAction_login.action";
cdata = {"name":$("#name").val(),"password":$("#password").val()};
alert(123);
$.ajax({
url:"userAction_login.action",
type:"post",
dataType:"text",
data:cdata,
success:function(data){
if(data=="1"){
alert("success");
}else{
alert("用户名密码错误");
}
}
});
}
用的是jquery-1.7.2.min.js
strusts配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
   
<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="login" namespace="/login" extends="struts-default">
<action name="userAction_*" class="com.user.userview.UserAction" method="{1}">
</action>
</package>

</struts>
java代码:
package com.user.userview;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.user.userservice.UserService;

public class UserAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private HttpServletRequest request = ServletActionContext.getRequest();
private HttpServletResponse response = ServletActionContext.getResponse();

public void login() {
System.out.println("ww");

PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
String name = request.getParameter("name");
String password = request.getParameter("password");
UserService userService = new UserService();
if (userService.login(name, password)) {
out.print(1);
} else {
out.print(0);
}
out.close();
}
}
问题:由浏览器向后台传数据,无法进入到java代码中
那位大