1.写一个继承ActionServlet的类A。在该类A中重写HttpServlet的service方法,然后调用ActionServlet中的process方法,然后从requst中得到要返回给前台的值value,如果不为空,则用response. getWriter().print(value);
2.修改web.xml中的名为action的servlet中servlet-class路径为类A的包名+A.
3.在业务Action中把execute方法的返回值设为null,把要返回到前台的对象存放到request中,使类A中的service方法能得到。
4.在写Struts-config.xml文件的action时,不用写forward。
例子:
例子中使用json与前台通信。
继承ActionServlet的ControllerServlet.java
1package com.kai.struts;
2
3import java.io.IOException;
4
5import javax.servlet.ServletException;
6import javax.servlet.http.HttpServletRequest;
7import javax.servlet.http.HttpServletResponse;
8
9import net.sf.json.JSONObject;
10
11import org.apache.struts.action.ActionServlet;
12
13public class ControllerServlet extends ActionServlet {
14 private static final long serialVersionUID = -1967288521493475334L;
15 /** *//***
16 * 1.重写HttpServlet的service方法;调用ActionServlet中的process方法。
17 * 2.修改web.xml中的action servlet-class为com.kai.struts.ControllerServlet
18 */
19 protected void service(HttpServletRequest request, HttpServletResponse response)
20 throws ServletException, IOException {
21 //调用ActionServlet 中的process方法
22 this.process(request, response);
23 //从request中读取返回给ajax的数据JSONObject
24 JSONObject ajaxResult = (JSONObject)request.getAttribute("ajaxResult");
25 //如果此数据存在,则返回
26 if(ajaxResult!=null){
27 response.getWriter().print(ajaxResult);
28 }
29 }
30}
二.login.jsp中.当username失去焦点时触发事件。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/login.js"></script></head>
<body>
<form action="login.do" method="post">
<input name="username" type="text" onblur="validateUser()" id="username"><br>
<input name="password" type="text"><br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>
login.js
1var xmlHttp;
2
3function createXmlHttpRequest(){
4 if(window.XmlHttpRequest){
5 xmlHttp = new XmlHttpRequest();
6 }else if(window.ActiveXObject){
7 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
8 }
9}
10
11function validateUser(){
12 createXmlHttpRequest();
13 xmlHttp.onreadystatechange=handleStateChange;
14 xmlHttp.open("POST","validataUser.do",true);
15 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
16 var content = "username="+document.getElementById("username").value;
17 xmlHttp.send(content);
18}
19
20function handleStateChange(){
21 if(xmlHttp.readystate==4&&xmlHttp.status==200){
22 var array;
23 //把返回结果转化为对象
24 eval("array="+xmlHttp.responseText);
25 alert(array);
26 alert(array.succ);
27 alert(array.name);
28 alert(array.pass);
29 }
30}
三.修改web.xml中action
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.kai.struts.ControllerServlet</servlet-class>
<init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.kai.struts.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="com.kai.struts.loginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
<action path="/validataUser"
type="com.kai.struts.validataUserAction"
name="loginForm"
scope="request"
>
</action>
</action-mappings>
</struts-config>
validataUserAction.java
1package com.kai.struts;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11import net.sf.json.JSONObject;
12
13import org.apache.struts.action.Action;
14import org.apache.struts.action.ActionForm;
15import org.apache.struts.action.ActionForward;
16import org.apache.struts.action.ActionMapping;
17
18public class validataUserAction extends Action {
19 private boolean succ=false;
20 @Override
21 public ActionForward execute(ActionMapping mapping, ActionForm form,
22 HttpServletRequest request, HttpServletResponse response)
23 throws Exception {
24 LoginActionForm actionForm=(LoginActionForm)form;
25 //如果username等于kai则设succ为true;
26 if(actionForm.getUsername().equalsIgnoreCase("kai")){
27 succ=true;
28 }
29 List aa=new ArrayList();
30 aa.add("kai");
31 aa.add("haha");
32 String[] bb = {"aaa","bbb"};
33 //创建返回的JSONObject对象
34 JSONObject result = new JSONObject();
35 result.put("succ", succ);
36 result.put("name", aa);
37 result.put("pass", bb);
38 //把result放到request中,key值必需为ajaxResult
39 request.setAttribute("ajaxResult", result);
40 //一定要返回空值才能使servlet不跳转
41 return null;
42 }
43
44}
LoginActionForm.java用于收集表单数据
1package com.kai.struts;
2
3import org.apache.struts.action.ActionForm;
4
5public class LoginActionForm extends ActionForm {
6 private static final long serialVersionUID = -5121106589990012711L;
7 private String username;
8 private String password;
9 public String getUsername() {
10 return username;
11 }
12 public void setUsername(String username) {
13 this.username = username;
14 }
15 public String getPassword() {
16 return password;
17 }
18 public void setPassword(String password) {
19 this.password = password;
20 }
21}