你好我是Struts2中的新手,Ajax我正在尝试在Struts2 + ajax中创建一个hello world当我按下同一页面中的按钮但我看到一个动作中的hello world,问题出在哪里? 对不起,我正在学习英语,thx。
的index.jsp
<?xml version="1.0" encoding="UTF-8"?>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" >
<head>
<sj:head jqueryui="true"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>DEMO AJAX</title>
<sj:head jqueryui="true"/>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
// Processing hello button
$('.buttonhello').click(function() {
$.ajax({
type : "POST",
url : '${pageContext.request.contextPath}/index.html',
success : function(response) {
$('.result').html(response);
}
});
});
});
</script>
<s:form action="ajaxTest" theme="xhtml">
<s:submit value="submit" />
</s:form>
</body>
</html>
我知道这部分不好不知道写什么
$('.buttonhello').click(function() {
struts.xml中
<action name="ajaxTest" class="com.atorresbr.actions.AjaxTest" method="hello">
<result name="success" >index.jsp</result>
</action>
AjaxTest.java(行动)
package com.atorresbr.actions;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AjaxTest extends ActionSupport {
private static final long serialVersionUID = 1L;
public void hello(){
try {
HttpServletResponse response =
ServletActionContext.getResponse();
response.setContentType("text/plain;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("Hello Ajax");
out.flush();
} catch (Exception e){
}
}
}
我已经阅读了很多教程,但这使用了dojo,我读过这是一个过时的版本。非常感谢你。