我想在struts中使用jquery ajax选择下拉值时显示数据库值。但是ajax没有调用url。
我的jsp页面:
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<html>
<head><title>welcome</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#show').click(function(event) {
var name = $('#UserName').value;
$.get('ajax.do', { userName : name }, function(responseText) {
$('#ajaxResponse').text(responseText);
});
});
});
</script>
</head>
<body>
<div id="details">
Select Name:
<select name="whatever" id="submit">
<logic:iterate id="slist" name="MapForm" property="list">
<option id="UserName" value="UserName"><bean:write name="slist" /></option>
</logic:iterate>
</select><br><br>
<button id="show">show details</button><br><br>
</div>
<div id="ajaxResponse">User info will be listed here...</div>
</body>
</html>
我的行动类是:
package com.map;
import java.io.PrintWriter;
import java.sql.DriverManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AjaxAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception
{
PrintWriter out = response.getWriter();
try{
String username=request.getParameter("userName");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/nithya","root","root");
java.sql.Statement statement = connection.createStatement() ;
String sql="select * from login where username='"+username+"' ";
java.sql.ResultSet resultset =statement.executeQuery(sql) ;
while(resultset.next()){
out.print("<FName:>"); resultset.getString("firstname");out.print("<br>");
out.print("<LName:>"); resultset.getString("lastname");out.print("<br>");
out.print("<Email:>"); resultset.getString("email");out.print("<br>");
out.print("<Mobile_num:>"); resultset.getString("phone");out.print("<br>");
out.print("<address:>"); resultset.getString("address");out.print("<br>");
}
}
catch(Exception e)
{
out.println("wrong entry"+e);
}
return null;
}
}
在动作类中我使用out.print()。还有其他方法可以显示吗?
的struts-config
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="AjaxForm" type="com.map.AjaxForm"/>
</form-beans>
<action-mappings>
</forward>
</action>
<action name="AjaxForm" path="/ajax" type="com.map.AjaxAction">
</action>
</action-mappings>
</struts-config>