使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互

写博客的时候,我在想,是不是以后也有人来问我我向别人提过的问题。认识了一些人之后,在技术还有其他的一些问题上的疑问,在有经验的人身上得到一些启示。然后将自己的东西写出来之后,又有人来问我,这是一件很奇妙的事情。我曾经受过别人帮助,当别人有求的时候,我也希望能把自己会的说给他人听。


继上篇:《使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互》之后,让ajax与Servlet进行交互。接下来是与struts2的结合。


struts的相关知识不多讲,没学的补上即可。


struts所需jar包。



web.xml加上strut2过滤器。

<?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>ZeroToOne</display-name>
	<!-- <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> 
		<welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> 
		<welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> 
		</welcome-file-list> -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/* </url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>Hello</servlet-name>
		<servlet-class>Servlet.HelloServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/helloServlet</url-pattern>
	</servlet-mapping>
</web-app>

根目录添加struts.xml

<?xml version="1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.locale" value="zh_CN" />
	<constant name="struts.i18n.encoding" value="utf-8" />
	<package name="default" namespace="/" extends="struts-default">
		<default-action-ref name="index" />
		<global-results>
			<result name="error">/error.jsp </result>
			<result name="success">/hello.jsp </result>
		</global-results>
	<global-exception-mappings>
		<exception-mapping exception="java.lang.Exception"
			result="error" />
	</global-exception-mappings> 

		<action name="ajaxaction" class="action.AjaxAction" method="ajax">
			<result name="success">/hello.jsp</result>
		</action>
	</package>

	<!-- <include file="example.xml" /> -->
	<!-- Add packages here -->

</struts>


新建AjaxAction实现Struts2的Action接口。像第一篇文章那样直接用PrintWriter直接输出,
return Success.

AjaxAction类

public class AjaxAction implements Action{
	public String ajax() throws Exception {
		System.out.println("action execute");
		ServletActionContext.getResponse().getWriter().println("struts data response to ajax");
		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		return null;
	}
}

由于struts过滤器的存在,ajax.html点击ajax按钮的时候,请求ajaxaction,命名空间为/,找到namespace为/的
package标签。

根据struts.xml的配置,找到action标签中name为ajaxaction的。请求action.AjaxAction的ajax方法。

执行完之后result为success,跳转hello.jsp页面。


hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>the response do not response to ajax</h3>
</body>
</html>


执行过程中会报:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
解决:project-》properties-》Deployment Assembly-》add-》java build path entries,build path是加载到class path里面,tomcat在刚启动的时候,filter需要经过struts,
而本身的web-inf/lib没有jar包所以报类不在的错误。所以需要手动复制进lib目录或者用上面的方法。

然后我们就能得到像ajax与Servlet交互那样的结果,得到PrintWriter所输出的字符串。


但是结果却没有拿到ServletActionContext.getResponse().getWriter().println("struts data response to ajax")输出的字符串。

xmlhttp.responseText拿到的却是hello.jsp的内容。


那我们就需要让struts2访问此方法后不跳转到hello.jsp。找了下有Action.NONE这个值。

在struts的官方guides中:https://struts.apache.org/docs/result-configuration.html

有它的说明,Returning ActionSupport.NONE (or null) from an Action class method causes the results processing to be skipped. This is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.


从Action类中返回ActionSupport.NONE(或者null)可以让result的处理省略,当你想完全控制result处理,像通过HttpServletResponse OutputStream直接输出的时候很有用。

是的,我们就是要这样用。

将AjaxAction的返回语句改为:

ServletActionContext.getResponse().getWriter().println("struts data response to ajax");
		return Action.NONE;

去除struts.xml中的:

<result name="success">/hello.jsp</result>

最后让struts与ajax交互。


代码下载:http://download.csdn.net/detail/iaiti/9118327

代码


除了用none返回外,其实也可以正常使用success这些普通的字符串,当然结果就像前面说的一样跳转到特定页面。

但是换一下思路,将我们想要输出的东西放到这个页面也是可以的。

如果将Action类里面的东西放到指定页面呢,可以通过ActionContext将特定内容放入,再从页面使用Request.getAttribute将这个特定内容的值提取出来。

再用out.write输出,也是可以。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iaiti

赏顿早餐钱~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值