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

actionmapping

阅读更多
1、struts-config.xml文件中,每个<action>标签对应一个ActionMapping实例
2、了解<action>标签中的forward和unknown属性的含义
3、了解采用jstl和struts标签保持页面数据



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>
	
	<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"/>
			 -->
			 <forward name="error" path="/login.jsp"/>
		</action>
	
		<action path="/login2"
				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"/>
			 -->
			 <forward name="error" path="/login_struts_tag.jsp"/>
		</action>
		
		<action path="/login1"
				forward="/login.jsp"
		></action>
		
		<action path="/testunknown"
				unknown="true"
				forward="/testunknown.jsp"
		></action>
		
		<action path="/loginstrutstag"
				forward="/login_struts_tag.jsp"
		></action>
	</action-mappings>
</struts-config>


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);
			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();
		}
	}
	
}


PasswordErrorException.java

package com.bjsxt.struts;

public class PasswordErrorException extends RuntimeException {

}


UserNotFoundException.java

package com.bjsxt.struts;

public class UserNotFoundException extends RuntimeException {

}



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>
  	<h1>测试ActionMapping</h1>
    <a href="login1.do">登录(采用jstl)</a><br>
    <a href="loginstrutstag.do">登录(采用struts tag)</a>
  </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" value="${loginForm.username }"><br>
		密码:<input type="password" name="password" value="${loginForm.password }"><br>
		<input type="submit" value="登录">
	</form>
</body>
</html>


login_struts_tag.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>    
<!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>用户登录(采用struts标签)</h1>
	<hr>
	<html:form action="login2.do" method="post">
		用户:<html:text property="username"/><br>
		密码:<html:password property="password"/><br>
		<html:submit value="登录"/>
	</html:form>
</body>
</html>



test_unknown.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>



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>


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>
分享到:
评论

相关推荐

    ActionForward and ActionMapping

    ActionForward and ActionMapping

    Struts1教程之ActionMapping_动力节点Java学院整理

    Struts1教程之ActionMapping Struts1教程之ActionMapping是一个重要的概念,在Struts1框架中扮演着关键角色。ActionMapping是Struts1框架中的一个核心组件,它负责处理用户请求并将其映射到对应的Action类中。今天...

    用MockStrutsTestCase测试action测试类

    2. **测试环境准备**:MockStrutsTestCase会自动配置一个模拟的Servlet环境,包括ActionServlet、RequestProcessor和ActionMapping等。你需要在测试类中指定待测试的Action类,通常通过覆盖`getActionClass()`方法...

    actionmaping

    标题中的"actionmapping"指的是Struts框架中的一个关键概念,ActionMapping。在Struts框架中,ActionMapping是用来定义用户请求如何映射到特定的Action类的。它在处理Web应用程序的请求分发中起着核心作用,使得不同...

    外文翻译 stus MVC

    The ActionMapping contains the knowledge of how a specific event maps to specific Actions. The ActionServlet (Command) passes the ActionMapping to the Action class via the perform() method. This ...

    ssh(structs,spring,hibernate)框架中的上传下载

    Struts+Spring+Hibernate实现上传下载    本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0 ...

    strutsinaction

    ### Struts in Action:设计ActionMapping #### 7.1 进入ActionMapping Model2架构鼓励在同一个应用中使用servlet和JSP页面。在Model2架构下,总是从调用一个servlet开始,servlet处理业务逻辑并将控制转到相应的...

    servelt模拟struts1框架

    ActionForm用于封装请求参数,Action是具体的业务处理类,而ActionMapping则定义了请求与Action之间的映射关系。 在模拟Struts1的过程中,我们将创建一个Servlet类,这个类将扮演ActionServlet的角色。首先,我们...

    js宝方法方法法笑嘻嘻典

    【JS宝典:深入理解ActionForward与ActionMapping在Struts2框架中的作用】 在Web开发领域,Struts2框架以其强大的MVC模型而备受推崇,它源于Webwork2框架,后者是一个基于命令模式的Web框架XWork。Webwork的核心...

    Struts课件.doc

    3. 如果配置了Action类,ActionServlet会创建Action类的实例,并调用其execute方法,传入ActionMapping、ActionForm、HttpServletRequest和HttpServletResponse这四个参数。 4. execute方法执行后,ActionServlet...

    Struts教程

    `Action`接口定义了所有业务逻辑处理的入口点,其`execute`方法是主要的执行方法,接收`ActionMapping`, `ActionForm`, `HttpServletRequest`, 和`HttpServletResponse`作为参数,处理业务逻辑后返回一个`Action...

    Struts的工作原理

    它会读取应用目录下的`struts-config.xml`配置文件,从中提取出关于Action、ActionMapping、ActionForm等配置信息,并存储在相应的对象中。 当一个用户的请求到达服务器,`ActionServlet`首先会查找与请求路径相...

    Struts2 S2-033漏洞分析1

    RestActionMapper.java 的 public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) 的方法获取的,其中会通过 handleDynamicMethodInvocation 方法设置 ActionMapping ...

    王者归来之达内内部绝密Struts课件.doc

    3. 调用`ActionMapping`中指定的`Action`类的`execute`方法,传递`ActionMapping`、`ActionForm`、`HttpServletRequest`和`HttpServletResponse`作为参数。 4. `execute`方法返回一个`ActionForward`对象,`Action...

    Struts framework的工作原理和组件Struts framework的工作原理和组件

    3. **查找ActionMapping**:根据ActionMapping中配置的信息(如路径、类型等),ActionServlet创建相应的Action对象。 4. **创建ActionForm**:如果ActionMapping中指定了ActionForm,则创建相应的ActionForm对象并...

    struts笔记

    3. 如果在`ActionMapping`中定义了`Action`类,那么`ActionServlet`会实例化该`Action`类,并调用其`execute`方法,同时传入`ActionMapping`、`ActionForm`、`HttpServletRequest`和`HttpServletResponse`作为参数。...

    学生管理的Struts框架开发实例

    3. **ActionMapping**: 在配置文件(如struts-config.xml)中,ActionMapping定义了URL到Action的映射。例如,"/student/add"请求可能映射到AddStudentAction,使得用户在提交添加学生的表单时,Struts知道调用哪个...

    struts2控制器源码讲解

    2. **Action映射**:接着,`FilterDispatcher`通过`actionMapper`对象获取与请求关联的`ActionMapping`。`ActionMapping`是Struts2中的一个重要概念,它定义了请求URL与特定Action类之间的映射关系。`actionMapper`...

    struts 2 之旅 - 简单的程序 登陆

    Struts 2框架的主要组件包括Action类、ActionMapping、Result、Interceptor等。Action类是业务逻辑的载体,通常对应一个用户请求;ActionMapping则定义了请求URL与Action类的映射关系;Result负责处理Action执行后的...

Global site tag (gtag.js) - Google Analytics