现在的位置: 首页 > 综合 > 正文

对Action中返回的少量字符串的处理方式(不写转发页面)

2018年05月20日 ⁄ 综合 ⁄ 共 3421字 ⁄ 字号 评论关闭

在action中一般的处理方式是:核心servlet或者Filter接收到用户请求后,通常会对用户请求进行简单的预处理,比如解析,封装参数。然后通过反射来创建Action的实例。并调用action指定的方法(struts1中为execute方法,struts2中为任意方法)来处理用户请求,并通过配置文件转发到对应的页面。这样做对一些复杂的处理固然很好,但是像登录这类只返回true或false的字符串二样,就没有必要用这种方式了。可以再action里面调用servlet
api,直接打印输出即可。

下面是一个关于ajax交互action实现简单登录的小例子。该例子中在struts标签<action>中没有配置<result>标签进行跳转,而是在action里面执行打印输出。这样简化了代码的编写。

1.登录界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>

</head>
<script type="text/javascript">
	$(function() {
		$("#button1").click(function() {
			//alert("aaa");
			var username = $("#username").val();
			var password = $("#password").val();
			$.get("test.action", {
				"username" : username,
				"password":password
			}, function(data) {
				if(data.indexOf("true") >-1){
					window.location.href  = "master.jsp";
				}
				else{
					alert("登录失败");
				}
			},"text");
		});
	});
</script>
<body>
	姓名:
	<input type="text" name="username" id="username"> 密码:
	<input type="text" name="password" id="password">
	<input type="button" id="button1" value="提交">
</body>
</html>


test.action对应的代码

package com.xzy.demo.struts.test;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Test extends ActionSupport {
	HttpServletRequest request = ServletActionContext.getRequest(); //得到servlet里面的request
	HttpServletResponse response = ServletActionContext.getResponse(); //得到servlet里面的response
	HttpSession session = request.getSession();
	
	public void login() throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			if(username.equals("xzy") && password.equals("123")){
				session.setAttribute("username", username);
				out.print("true");//通过out对象直接输出
			}else{
				out.print("false");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


登录成功跳转到master.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	response.setCharacterEncoding("utf-8");
	request.setCharacterEncoding("utf-8");
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'master.jsp' starting page</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

	<%
		try {
			if (session.getAttribute("username") != null) {
				out.print((String)session.getAttribute("username") + "已经登录");
			} else {
				out.print("未登录");
			}
		} catch (Exception e) {
			out.print(e.getMessage());
			e.printStackTrace();
		}
	%>
</body>
</html>


此时配置文件不需要配置result标签了。代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="struts" extends="struts-default">
		<action name="test" class="com.xzy.demo.struts.test.Test"
			method="login">
		</action>
	</package>
</struts>    

这样不仅简化了代码开发,适用于返回很少量的字符串时使用。


抱歉!评论已关闭.