如何从struts2应用程序中的AJAX调用url调用servlet?

时间:2015-02-11 07:12:01

标签: java javascript servlets struts2

我正在开发一个具有AJAX调用的简单Struts2应用程序。我正在尝试使用AJAX调用检查表单的每个字段,同时填充它。我想从servlet获得响应。为了调用servlet,我在struts.xml文件中使用了'exclude url',并在web.xml文件中包含了ajax调用模式。但是我的应用程序无法找到servlet。以下是代码段:

empForm.jsp:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="util.js" type="text/javascript"></script>
<script src="validate.js" type="text/javascript"></script>
<title>Employee Registration Form</title>
</head>

<body>
    <h1 align="center">Employee Registration Form</h1>
    <h2 align="left">Please provide your details in the appropriate fields. Fields marked with * are mandatory. </h2><br><br>
    <s:form action="register">
        <s:textfield id="username" name="username" label="Username*" />
        <s:textfield name="fullname" label="Full Name*" />
        <s:textfield name="emailid" label="Email*" />

validate.js:

window.onload = initPage;
alert("validate.js page");

function initPage() {
  document.getElementById("username").onblur = checkUsername;
  //document.getElementById("register").disabled = true;
  alert("in initPage();");
}

function checkUsername() {
    request=createRequest();
    if(request==null)
        alert("Unable to create request!");
    else{
        var theName = document.getElementById("username").value;
        var username = escape(theName);
        var url = "http://localhost:8080/PeopleManagement/checkName.do?username=" + username;
        //var url = "checkName.do?username=" + username;
        request.onreadystatechange = showUsernameStatus;
        request.open("GET",url,true);
        request.send(null);
        alert("url:" + url);
    }
    alert("in checkUsername();");
}

function showUsernameStatus(){
    if(request.readyState == 4){
        if(request.status == 200){
            alert("Status:" + request.statusText);
            alert("Response:" + request.responseText);
            if(request.statusText == "okay"){
                alert("allowed");
            }
            else
                alert("denied");
        }
    }
}

struts.xml中:

    

<package name="admin" extends="struts-default">
    <action name="Welcome">
        <result>/welcome.jsp</result>
    </action>
    <action name="Admin" class="com.admin.AdminUser" method="execute">
        <result name="input">/adminLogin.jsp</result>
        <result name="SUCCESS">/adminMenu.jsp</result>
    </action>
</package>
<package name="registration" extends="struts-default">
    <action name="register" class="com.emp.Employee" method="execute">
        <result name="input">empForm.jsp</result>
        <result name="error">/registrationError.jsp</result>
        <result name="SUCCESS">/registrationSuccess.jsp</result>
    </action>
    <action name="viewemp" class="com.emp.ViewEMP" method="execute">
        <result name="error">/viewError.jsp</result>
        <result name="SUCCESS">/list.jsp</result>
    </action>
</package>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>PeopleManagement</display-name>
  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>

  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
  </filter>

  <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>UserValidator</servlet-name>
    <servlet-class>com.validation.ValidateUser</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UserValidator</servlet-name>
    <url-pattern>/checkName.do*</url-pattern>
  </servlet-mapping>

</web-app>

请帮帮我。

1 个答案:

答案 0 :(得分:2)

您需要在web.xml中正确映射您的Servlet

<servlet-mapping>
  <servlet-name>UserValidator</servlet-name>
  <url-pattern>/PeopleManagement/checkName.do</url-pattern>
</servlet-mapping>

.........

[编辑] 您还必须编辑/ * to / struts拦截器servlet。这里解释了不同之处 - Difference between / and /* in servlet mapping url pattern