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

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进行如下配置



  MyStruts1Prj
  
  
  	action
  	org.apache.struts.action.ActionServlet
  	
	
		config
		/WEB-INF/conf/struts-config.xml
	
	
		debug
		3
	
	
		detail
		3
	
	
	0
  
  
  
  	action
  	*.do
  
  
  
    index.jsp
  


4、新建登陆页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



<%
	String basePath = request.getContextPath();
%>

登陆页面


	

登陆页面


userName:
passWord:


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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登陆成功页面


	

欢迎[<%=request.getAttribute("userName") %>]登陆成功!


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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登陆失败页面


	

登陆失败!


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





	
		
		
	

	
		
			
			
		
	


 

 

OK,TKS!

你可能感兴趣的:(IT海洋——Web)