我一直试图让这段代码运转起来。基本的想法是我想在id为" user"的文本框时创建一个ajax请求。通过ajax调用返回服务器(apache)。
我在执行http://localhost:8080/CheckUserAvailabilityAction.do?username=ds
请求时得到的错误是404.
这是我的代码。 Struts2类CheckUserAvailabilityAction.java:
public class CheckUserAvailabilityAction extends ActionSupport implements
ServletRequestAware {
//struts action
public String execute() {
String output = "success";
/*
SOME CODE
*/
return output;
}
}
这是我的jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="js/scripts.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script>
var url = "HelloWorld.jsp";
function makeRequest(url) {
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
if (!httpRequest) {
return false;
}
httpRequest.onreadystatechange = alertContents();
httpRequest.open('GET', url);
httpRequest.send();
//Redirecting to the HelloUser.jsp
window.location.replace(url);
}
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
$(function(){
$("#username").blur("/CheckUserAvailability");
});
function checkUserAvailability(){
var name = $("#username").val();
$.get(
"/CheckUserAvailabilityAction.do",
{ 'username': name },
function () {
var response = $(data).find("response").text();
$("#response").hide();
$("#response").empty();
$("#response").append(response);
$("#response").show("slow");
}
);
}
</script>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New user - Registration form</title>
</head>
<body id="bodyStyling">
<%
if ("noSexSelected".equalsIgnoreCase(request
.getParameter("register"))) {
%>
<div id="registrationFailure">
<h4>Please select a Sex</h4>
</div>
<%
}
%>
<form name="myForm" action="checkUserNameAvailability" method="post">
<!-- onsubmit="return validateForm(this);" -->
<table>
<tr>
<td><h2 id="IntroductionReg">New User Registration</h2></td>
</tr>
<tr>
<td class="myTableDataStyling">Name:</td>
<td><input name="username" id="username" type="text"
onblur="checkUserAvailability()" /></td>
<td><label id="usrLabel"></label></td>
</tr>
<tr>
<td class="myTableDataStyling">Password:</td>
<td><input name="password" type="password" onclick= /></td>
</tr>
<tr>
<td class="myTableDataStyling">Confirm password:</td>
<td><input name="passwordConfirmation" type="password"
onclick="this.value''" /></td>
</tr>
<tr>
<td class="myTableDataStyling">Age:</td>
<td><input name="age" onclick="this.value=''" /></td>
</tr>
<tr>
<td class="myTableDataStyling">Sex:</td>
<td><select name="sex">
<option value="">- Select -</option>
<option value="M">M</option>
<option value="F">F</option>
</select>
</tr>
<tr>
<td class="myTableDataStyling">Address:</td>
<td><input name="address" onclick="this.value=''" /></td>
</tr>
<tr>
<td class="myTableDataStyling">E-mail:</td>
<td><input name="email" onclick="this.value=''" /></td>
</tr>
<tr>
</tr>
</table>
<input type="submit" value="Register user" />
</form>
<div id="outputDiv"></div>
<div id="response"></div>
</body>
</html>
这是我的struts.xml:
<?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>
<!-- Here, I declare the actions for the com.tcs.controller package classes -->
<package name="damo" extends="struts-default">
<action name="process" class="com.tcs.controller.User">
<result name="success">/process.jsp</result>
</action>
<action name="login" class="com.tcs.action.LoginAction">
<result name="success">HelloUser.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="register" class="com.tcs.action.RegisterUserAction">
<result name="success">process.jsp</result>
<result name="error">process.jsp</result>
</action>
<action name="checkUserNameAvailability" class="com.tcs.action.CheckUserAvailabilityAction">
<result name="success" type='redirect'>register.action</result>
<result name="error">process.jsp</result>
</action>
</package>
</struts>
我努力让我的checkUserAvailability函数发出有效请求,因为我对struts知之甚少,所以我迷失了方向。
非常感谢帮助
感谢。
答案 0 :(得分:0)
我认为您错过了网址中的项目名称。你能测试一下吗?
http://localhost:8080/[yourProjectName]/CheckUserAvailabilityAction.do?username=ds
这样,如果您的工作区中的项目被调用foo
,您就会调用网址
http://localhost:8080/foo/CheckUserAvailabilityAction.do?username=ds
答案 1 :(得分:0)