解析请求参数调用Action中的方法的struts1.x完整实例

时间:2022-09-29 12:12:24

1、简介

        通过扩展DispatchAction类,并复写其中的execute方法,来通过解析请求地址中的Method=?参数来调用相应Action中的方法,从而很好地实现了控制器的请求转发跳转。其中利用到了反射技术实现执行方法。下面以登陆为例。

2、在新建的web工程中添加以下struts1.x  jar包

        antlr-2.7.6.jar、commons-beanutils.jar、commons-digester.jar、commons-fileupload.jar、commons-logging-1.0.4.jar、commons-validator.jar、jakarta-oro.jar、struts.jar。

3、web.xml进行如下配置

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyStruts1Prj</display-name>
  
  <servlet>
  	<servlet-name>action</servlet-name>
  	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  	
	<init-param>
		<param-name>config</param-name>
		<param-value>/WEB-INF/conf/struts-config.xml</param-value>
	</init-param>
	<init-param>
		<param-name>debug</param-name>
		<param-value>3</param-value>
	</init-param>
	<init-param>
		<param-name>detail</param-name>
		<param-value>3</param-value>
	</init-param>
	
	<load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>action</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


4、新建登陆页面login.jsp

<%@ 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>
<%
	String basePath = request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆页面</title>
</head>
<body>
	<h1>登陆页面</h1>
	<hr>
	<form action="<%=basePath %>/login.do?method=login" method="post" >
		userName:<input id="userName" name="userName" type="text" /><br>
		passWord:<input id="passWord" name="passWord" type="password" /><br>
		<input type="submit" id="submit" name="submit" value="submit" />
	</form>
</body>
</html>


5、新建登陆成功后的跳转页面loginSucces.jsp

<%@ 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>登陆成功页面</title>
</head>
<body>
	<h1>欢迎[<%=request.getAttribute("userName") %>]登陆成功!</h1>
</body>
</html>


6、新建登陆失败后的跳转页面loginError.jsp

<%@ 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>登陆失败页面</title>
</head>
<body>
	<h1>登陆失败!</h1>
</body>
</html>


7、新建LoginActionForm.java

package com.lanp.webapp.form;

import org.apache.struts.action.ActionForm;

/**
 * 封装登陆表单数据的FORM类
 * @author LanP
 * @since 2012年4月11日23:07:09
 * @version v1.0
 */
@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {
	private String userName;
	
	private String passWord;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
}


8、扩展了DispatchAction类的BaseAction.java

package com.lanp.webapp.utils;

import java.lang.reflect.Method;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 * 经过自己封装后的Action,可以根据请求参数来调用相应的Action方法
 * @author LanP
 * @since 2012-4-12 21:41:14
 * @version v1.0
 */
public class BaseAction extends DispatchAction {

	@SuppressWarnings("rawtypes")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String actionMethod = request.getParameter("method");
		if(null != actionMethod && actionMethod.length() > 0) {
			Class[] clazzs = new Class[4];
			clazzs[0] = ActionMapping.class;
			clazzs[1] = ActionForm.class;
			clazzs[2] = HttpServletRequest.class;
			clazzs[3] = HttpServletResponse.class;
			
			Object[] parameters = new Object[4];
			parameters[0] = mapping;
			parameters[1] = form;
			parameters[2] = request;
			parameters[3] = response;
			
			Method method = getClass().getMethod(actionMethod, clazzs);
			ActionForward actionForward = (ActionForward)method.invoke(this, parameters);
			
			return actionForward;
		} else {
			System.out.println(">>> 请求地址中没有method=?参数 <<<");
		}
		
		return null;
	}
	
}


9、新建LoginAction.java

package com.lanp.webapp.action;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.lanp.webapp.form.LoginActionForm;
import com.lanp.webapp.utils.BaseAction;
/**
 * 处理登陆的Action类
 * @author LanP
 * @since 2012年4月11日23:07:09
 * @version v1.0
 */
public class LoginAction extends BaseAction {

	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println(">>> 执行登陆的login方法 <<<");
		String path = "error";
		LoginActionForm loginActionForm = (LoginActionForm)form;
		String userName = loginActionForm.getUserName();
		String passWord = loginActionForm.getPassWord();
		
		if(null != userName && "admin".equals(userName) && null != passWord && "admin".equals(passWord)) {
			path = "success";
			request.setAttribute("userName", userName);
		} else {
			path = "error";
		}
		return mapping.findForward(path);
	}
	
}


10、配置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="loginActionForm" type="com.lanp.webapp.form.LoginActionForm">
		</form-bean>
	</form-beans>

	<action-mappings>
		<action path="/login"
			type="com.lanp.webapp.action.LoginAction"
			name="loginActionForm"
			scope="request">
			<forward name="success" path="/jsp/loginSucces.jsp" />
			<forward name="error" path="/jsp/loginError.jsp" />
		</action>
	</action-mappings>
</struts-config>


 

 

OK,TKS!