- 浏览: 151215 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
hx0637:
楼主!小弟要面临面试了,能否指导下小弟?
面试 深圳一家公司的 -
kljismi:
你好,我现在正在开这项目的代码,但是我不明白@Privileg ...
权限管理模块分析 -
yzhw:
终于找到了
ImageSizer.java -
sunloveny:
国
struts国际化 -
jackotty:
谢谢楼主的分享
struts validator验证框架
1、struts-config.xml文件中,每个<action>标签对应一个ActionMapping实例
2、了解<action>标签中的forward和unknown属性的含义
3、了解采用jstl和struts标签保持页面数据
struts-config.xml
LoginAction.java
LoginActionForm.java
UserManager.java
PasswordErrorException.java
UserNotFoundException.java
index.jsp
login.jsp
login_struts_tag.jsp
test_unknown.jsp
login_success.jsp
login_error.jsp
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>
发表评论
-
drp_user
2009-01-11 15:10 1189采用DispathAction * 如果覆写DispathA ... -
编程式异常 -声明式异常
2009-01-11 11:28 14781、编程式异常 * 截获异常 * 创建相应的异常消息 * ... -
struts validator验证框架
2009-01-07 23:35 2902struts validator验证框架 1、配置: * ... -
struts国际化
2009-01-07 23:18 66631、struts国际化的配置 * 在struts-confi ... -
了解Locale
2009-01-07 23:01 12721、了解缺省Locale是由操作系统决定的,Locale是由语 ... -
ActionForward的使用
2009-01-07 22:13 41991、理解全局和局部ActionForward的概念 2、red ... -
分步收集,ActionForm的scope=session
2009-01-07 21:58 1731StartAction.java package com.b ... -
ActionForm
2009-01-07 20:06 14971、动态ActionForm 动态ActionForm是为了 ... -
DispatchAction.java
2009-01-07 19:48 1076/* * $Id: DispatchAction.java ... -
模式匹配
2009-01-07 19:41 779UserAction.java package com. ... -
jstl标签库的配置
2009-01-07 18:51 3412jstl标签库的配置 * 将jstl.jar和standar ... -
1.2.9 RequestProcessor.java
2008-12-27 17:47 1301/* * $Id: RequestProcessor.ja ... -
1.2.9 ActionServlet.java
2008-12-27 17:43 1206/* * $Id: ActionServlet.java ... -
struts_login
2008-12-27 17:32 9071、配置struts * 拷贝struts lib下的所有j ... -
struts_trainnig_cal
2008-12-26 23:17 666CalAction.java package com.bj ... -
bjsxt test_servlet
2008-12-26 22:46 1126TestServlet.java package com. ...
相关推荐
ActionForward and ActionMapping
Struts1教程之ActionMapping Struts1教程之ActionMapping是一个重要的概念,在Struts1框架中扮演着关键角色。ActionMapping是Struts1框架中的一个核心组件,它负责处理用户请求并将其映射到对应的Action类中。今天...
2. **测试环境准备**:MockStrutsTestCase会自动配置一个模拟的Servlet环境,包括ActionServlet、RequestProcessor和ActionMapping等。你需要在测试类中指定待测试的Action类,通常通过覆盖`getActionClass()`方法...
标题中的"actionmapping"指的是Struts框架中的一个关键概念,ActionMapping。在Struts框架中,ActionMapping是用来定义用户请求如何映射到特定的Action类的。它在处理Web应用程序的请求分发中起着核心作用,使得不同...
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 ...
Struts+Spring+Hibernate实现上传下载 本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本: •Struts 1.2 •Spring 1.2.5 •Hibernate 3.0 ...
### Struts in Action:设计ActionMapping #### 7.1 进入ActionMapping Model2架构鼓励在同一个应用中使用servlet和JSP页面。在Model2架构下,总是从调用一个servlet开始,servlet处理业务逻辑并将控制转到相应的...
ActionForm用于封装请求参数,Action是具体的业务处理类,而ActionMapping则定义了请求与Action之间的映射关系。 在模拟Struts1的过程中,我们将创建一个Servlet类,这个类将扮演ActionServlet的角色。首先,我们...
【JS宝典:深入理解ActionForward与ActionMapping在Struts2框架中的作用】 在Web开发领域,Struts2框架以其强大的MVC模型而备受推崇,它源于Webwork2框架,后者是一个基于命令模式的Web框架XWork。Webwork的核心...
3. 如果配置了Action类,ActionServlet会创建Action类的实例,并调用其execute方法,传入ActionMapping、ActionForm、HttpServletRequest和HttpServletResponse这四个参数。 4. execute方法执行后,ActionServlet...
`Action`接口定义了所有业务逻辑处理的入口点,其`execute`方法是主要的执行方法,接收`ActionMapping`, `ActionForm`, `HttpServletRequest`, 和`HttpServletResponse`作为参数,处理业务逻辑后返回一个`Action...
它会读取应用目录下的`struts-config.xml`配置文件,从中提取出关于Action、ActionMapping、ActionForm等配置信息,并存储在相应的对象中。 当一个用户的请求到达服务器,`ActionServlet`首先会查找与请求路径相...
RestActionMapper.java 的 public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) 的方法获取的,其中会通过 handleDynamicMethodInvocation 方法设置 ActionMapping ...
3. 调用`ActionMapping`中指定的`Action`类的`execute`方法,传递`ActionMapping`、`ActionForm`、`HttpServletRequest`和`HttpServletResponse`作为参数。 4. `execute`方法返回一个`ActionForward`对象,`Action...
3. **查找ActionMapping**:根据ActionMapping中配置的信息(如路径、类型等),ActionServlet创建相应的Action对象。 4. **创建ActionForm**:如果ActionMapping中指定了ActionForm,则创建相应的ActionForm对象并...
3. 如果在`ActionMapping`中定义了`Action`类,那么`ActionServlet`会实例化该`Action`类,并调用其`execute`方法,同时传入`ActionMapping`、`ActionForm`、`HttpServletRequest`和`HttpServletResponse`作为参数。...
3. **ActionMapping**: 在配置文件(如struts-config.xml)中,ActionMapping定义了URL到Action的映射。例如,"/student/add"请求可能映射到AddStudentAction,使得用户在提交添加学生的表单时,Struts知道调用哪个...
2. **Action映射**:接着,`FilterDispatcher`通过`actionMapper`对象获取与请求关联的`ActionMapping`。`ActionMapping`是Struts2中的一个重要概念,它定义了请求URL与特定Action类之间的映射关系。`actionMapper`...
Struts 2框架的主要组件包括Action类、ActionMapping、Result、Interceptor等。Action类是业务逻辑的载体,通常对应一个用户请求;ActionMapping则定义了请求URL与Action类的映射关系;Result负责处理Action执行后的...