`
xxp3369
  • 浏览: 151248 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ActionForward的使用

阅读更多
1、理解全局和局部ActionForward的概念
2、redirect的使用
3、struts-config.xml文件不允许动态修改
4、理解动态ActionForward,动态的ActionForward是可以运行期修改的


LoginAction.java

package com.bjsxt.struts;

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

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

/**
 * 用户登录的Action
 * @author Administrator
 *
 */
public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm laf = (LoginActionForm)form;
		String username = laf.getUsername();
		String password = laf.getPassword();
//		if ("admin".equals(username) && "admin".equals(password)) {
//			//转向到登录成功页面
//			request.setAttribute("username", username);
//			return mapping.findForward("success");
//		}else {
//			//转向到登录失败页面
//			return mapping.findForward("error");
//		}
		String errorInfo = "";
		try {
			UserManager.getInstance().login(username, password);
			//request.setAttribute("username", username);
			request.getSession().setAttribute("user", username);
			return mapping.findForward("success");
		}catch(UserNotFoundException unfe) {
			unfe.printStackTrace();
			errorInfo = "用户不能找到,用户名称=[" + username + "]";
		}catch(PasswordErrorException pee) {
			pee.printStackTrace();
			errorInfo = "密码错误";
		}
		request.setAttribute("errorinfo", errorInfo);
		return mapping.findForward("error");
	}

}


LoginActionForm.java


package com.bjsxt.struts;

import javax.servlet.http.HttpServletRequest;

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

/**
 * 登录的ActionForm,ActionForam是做数据收集的,
 * 
 * ActionForm中的属性必须和表单中输入域的名称一致
 * @author Administrator
 *
 */
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;
	}
	
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		System.out.println("----------LoginActionForm.reset()-----------");
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		System.out.println("----------LoginActionForm.validate()-----------");
		return null;
	}
	
}


UserManager.java

package com.bjsxt.struts;

public class UserManager {

	private static UserManager instance = new UserManager();
	
	private UserManager() {}
	
	public static UserManager getInstance() {
		return instance;
	}
	
	public void login(String username, String password) {
		if (!"admin".equals(username)) {
			throw new UserNotFoundException();
		}
		if (!"admin".equals(password)) {
			throw new PasswordErrorException();
		}
	}
	
}



MustLoginAction.java


package com.bjsxt.struts;

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

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

public class MustLoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		if (request.getSession().getAttribute("user") == null) {
			
			ActionForward af = mapping.findForward("login");
			
			//struts-config.xml文件不能修改
			//af.setRedirect(false);
			return af;
			
			//重定向
			//response.sendRedirect(request.getContextPath() + "/login.jsp");
			//return null;
		}
		return mapping.findForward("success");
	}

}


DynaActionForwardTestAction.java


package com.bjsxt.struts;

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

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

public class DynaActionForwardTestAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String page = request.getParameter("page");
//		ActionForward af = null;
//		if ("1".equals(page)) {
//			af = mapping.findForward("page1");
//		}else if ("2".equals(page)) {
//			af = mapping.findForward("page2");
//		}
//		return af;
		ActionForward af = new ActionForward();
		af.setPath("/page" + page + ".jsp?name=Tom");
		return af;
	}

}



PasswordErrorException.java

package com.bjsxt.struts;

public class PasswordErrorException extends RuntimeException {

}


UserNotFoundException.java


package com.bjsxt.struts;

public class UserNotFoundException extends RuntimeException {

}


struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

	<form-beans>
		<form-bean name="loginForm" type="com.bjsxt.struts.LoginActionForm"/>
	</form-beans>
	
	<global-forwards>
		<forward name="login" path="/login.jsp" redirect="true"/>
	</global-forwards>
	
	<action-mappings>
		<action path="/login"
				type="com.bjsxt.struts.LoginAction"
				name="loginForm"
				scope="request"
				validate="false"
		>
			<forward name="success" path="/login_success.jsp"/>
			<forward name="error" path="/login_error.jsp"/>
		</action>
		
		<action path="/mustlogin"
				type="com.bjsxt.struts.MustLoginAction"
		>
			<!-- 
			<forward name="login" path="/login.jsp" redirect="true"/>
			 -->
			<forward name="success" path="/mustlogin.jsp"/>
		</action>
		
		<action path="/dynaactionforward"
				type="com.bjsxt.struts.DynaActionForwardTestAction"	
		>
		<!-- 
			<forward name="page1" path="/page1.jsp"/>
			<forward name="page2" path="/page2.jsp"/>
		 -->	
		</action>
	</action-mappings>
</struts-config>


index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <a href="login.jsp">登录</a><br>
    <a href="mustlogin.do">访问受保护的页面</a><br>
    <p>
    <li>动态ActionForward测试</li><br>
    <form action="dynaactionforward.do" method="post">
    	页面:<input type="text" name="page"><br>
    	<input type="submit" value="提交">
    </form>
  </body>
</html>


login.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>用户登录</title>
</head>
<body>
	<h1>用户登录</h1>
	<hr>
	<form action="login.do" method="post">
		用户:<input type="text" name="username"><br>
		密码:<input type="password" name="password"><br>
		<input type="submit" value="登录">
	</form>
</body>
</html>


login_error.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>登录失败</title>
</head>
<body>
	<%=request.getAttribute("errorinfo") %>
</body>
</html>



login_success.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.struts.*" %>    
<!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=GB18030">
<title>登录成功</title>
</head>
<body>
	<%
		LoginActionForm laf = (LoginActionForm)request.getAttribute("loginForm");
	%>
	<%=laf.getUsername() %>,登录成功
</body>
</html>



mustlogin.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
这个是一个受保护的页面
</body>
</html>


page1.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
page1
</body>
</html>


page2.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
page2
</body>
</html>


page3.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
page3
</body>

</html>
分享到:
评论

相关推荐

    structs in action中文完全版\6. 连线 ActionForward.pdf

    - **理解ActionForward的最佳实践**:为了确保应用的健壮性和可维护性,开发者应当遵循一套关于ActionForward使用的最佳实践。例如,合理设计ActionForward的逻辑名称(name),这有助于提高代码的可读性和可维护性;...

    ActionForward and ActionMapping

    ActionForward and ActionMapping

    ActionForward与ForwardAction的功能

    理解ActionForward的作用,掌握如何在`struts-config.xml`中配置和在Action类中使用ActionForward。对于ForwardAction,需要理解其在JSP页面中的应用,以及如何通过它来实现页面的局部转发。 **总结** Action...

    struts_actionforward.rar_ActionForward_redirect

    ActionForward的使用 1、理解全局和局部ActionForward的概念 2、redirect的使用 3、struts-config.xml文件不允许动态修改 4、理解动态ActionForward,动态的ActionForward是可以运行期修改的

    ActionForward

    **ActionForward 概述** ...通过合理配置和使用 ActionForward,我们可以构建出灵活且易于维护的 Web 应用。理解和掌握 ActionForward 的工作原理对于深入理解 Struts 框架以及 MVC 设计模式至关重要。

    ActionForward和国际化

    ### ActionForward的理解与使用 #### 默认行为:转发 `ActionForward`是Struts2框架中用于控制程序流程的重要对象之一。当一个Action执行完毕后,可以通过返回一个`ActionForward`实例来告诉Struts2下一步应该做...

    Action与Forward属性的含义

    Action与Forward属性的含义

    关于action 不能用forward的问题

    在探讨“关于action不能用forward的问题”这一主题时,我们主要关注的是在Struts2框架中如何正确地使用forward机制来实现页面跳转,并解决在实际应用过程中遇到的相关问题。 ### Struts2框架简介 Struts2是基于MVC...

    struts入门,最适合新手上路

    5. **使用ActionMapping和ActionForward的关系**:在Struts1中,ActionMapping对象包含了Action的配置信息,包括ActionForward。当Action执行完成后,会通过ActionMapping找到对应的ActionForward来决定接下来的流程...

    Ajax异步请求action接受json案例

    本文将深入探讨如何利用Ajax进行异步请求,特别是针对action的调用,并接收JSON(JavaScript Object Notation)数据,最后将这些数据展示在页面上。 首先,我们需要理解Ajax的基本工作原理。Ajax的核心是...

    StrutsAPI(java)

    在請求來臨後,RequestProcessor 根據請求URI呼叫對應的Action物件,將工作交給它,並在最後由Action物件得到一個ActionForward物件, ActionServlet使用ActionForward得知將流程forward至指定的資源。 當請求到達...

    JSP之使用jsp:forward实现用户信息验证的页面跳转

    本篇文章将深入探讨`jsp:forward`的使用方法以及在用户信息验证中的应用。 首先,`jsp:forward`的语法结构如下: ```jsp &lt;jsp:forward page="targetPage.jsp" /&gt; ``` 其中,`page`属性是必需的,它指定了要转发的...

    js宝方法方法法笑嘻嘻典

    4. **运行期添加参数**:在Action或JSP页面中,可以通过ActionForward动态添加请求参数,例如使用`&lt;html:link&gt;`标签或JSTL的`&lt;c:url&gt;`标签,将参数值附加到转发路径中。 **ActionMapping** 1. **ActionMapping属性...

    struts in action

    当接收到一个请求时,`ActionServlet`会查找与请求路径匹配的`ActionMapping`,然后创建或重用对应的`ActionForm`对象,调用`Action`的`execute`方法,并根据执行结果选择合适的`ActionForward`跳转到相应的视图。...

    struts面试题

    `ActionForward`对象可以通过两种方式创建和使用: 1. **动态创建**:在`execute()`方法内部,开发者可以手动创建`ActionForward`实例,指定其逻辑名称、跳转路径以及是否进行重定向。例如,`return new Action...

    Struts学习----Action 的配置和使用(源码学习)

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 获取表单数据 LoginForm loginForm = (LoginForm) ...

    北京大部分公司的ssh面试题

    Execute()从ActionForm对象中获取数据,完成业务逻辑,返回一个ActionForward对象,ActionServlet再把客户请求转发给ActionForward对象指定的jsp组件,ActionForward对象指定的jsp生成动态的网页,返回给客户。

    Struts程序的执行流程.pdf

    1. ActionServlet:是Struts框架中的控制器,负责接收用户请求,调用Action的execute()方法处理请求,并根据返回的ActionForward对象进行页面跳转。 2. Action:是一个接口,具体的业务逻辑由实现了这个接口的...

Global site tag (gtag.js) - Google Analytics