- 浏览: 151248 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
hx0637:
楼主!小弟要面临面试了,能否指导下小弟?
面试 深圳一家公司的 -
kljismi:
你好,我现在正在开这项目的代码,但是我不明白@Privileg ...
权限管理模块分析 -
yzhw:
终于找到了
ImageSizer.java -
sunloveny:
国
struts国际化 -
jackotty:
谢谢楼主的分享
struts validator验证框架
1、理解全局和局部ActionForward的概念
2、redirect的使用
3、struts-config.xml文件不允许动态修改
4、理解动态ActionForward,动态的ActionForward是可以运行期修改的
LoginAction.java
LoginActionForm.java
UserManager.java
MustLoginAction.java
DynaActionForwardTestAction.java
PasswordErrorException.java
UserNotFoundException.java
struts-config.xml
index.jsp
login.jsp
login_error.jsp
login_success.jsp
mustlogin.jsp
page1.jsp
page2.jsp
page3.jsp
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>
发表评论
-
drp_user
2009-01-11 15:10 1189采用DispathAction * 如果覆写DispathA ... -
编程式异常 -声明式异常
2009-01-11 11:28 14791、编程式异常 * 截获异常 * 创建相应的异常消息 * ... -
struts validator验证框架
2009-01-07 23:35 2902struts validator验证框架 1、配置: * ... -
struts国际化
2009-01-07 23:18 66641、struts国际化的配置 * 在struts-confi ... -
了解Locale
2009-01-07 23:01 12741、了解缺省Locale是由操作系统决定的,Locale是由语 ... -
actionmapping
2009-01-07 22:48 14091、struts-config.xml文件中,每个<ac ... -
分步收集,ActionForm的scope=session
2009-01-07 21:58 1732StartAction.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 1302/* * $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 667CalAction.java package com.bj ... -
bjsxt test_servlet
2008-12-26 22:46 1126TestServlet.java package com. ...
相关推荐
- **理解ActionForward的最佳实践**:为了确保应用的健壮性和可维护性,开发者应当遵循一套关于ActionForward使用的最佳实践。例如,合理设计ActionForward的逻辑名称(name),这有助于提高代码的可读性和可维护性;...
ActionForward and ActionMapping
理解ActionForward的作用,掌握如何在`struts-config.xml`中配置和在Action类中使用ActionForward。对于ForwardAction,需要理解其在JSP页面中的应用,以及如何通过它来实现页面的局部转发。 **总结** Action...
ActionForward的使用 1、理解全局和局部ActionForward的概念 2、redirect的使用 3、struts-config.xml文件不允许动态修改 4、理解动态ActionForward,动态的ActionForward是可以运行期修改的
**ActionForward 概述** ...通过合理配置和使用 ActionForward,我们可以构建出灵活且易于维护的 Web 应用。理解和掌握 ActionForward 的工作原理对于深入理解 Struts 框架以及 MVC 设计模式至关重要。
### ActionForward的理解与使用 #### 默认行为:转发 `ActionForward`是Struts2框架中用于控制程序流程的重要对象之一。当一个Action执行完毕后,可以通过返回一个`ActionForward`实例来告诉Struts2下一步应该做...
Action与Forward属性的含义
在探讨“关于action不能用forward的问题”这一主题时,我们主要关注的是在Struts2框架中如何正确地使用forward机制来实现页面跳转,并解决在实际应用过程中遇到的相关问题。 ### Struts2框架简介 Struts2是基于MVC...
5. **使用ActionMapping和ActionForward的关系**:在Struts1中,ActionMapping对象包含了Action的配置信息,包括ActionForward。当Action执行完成后,会通过ActionMapping找到对应的ActionForward来决定接下来的流程...
本文将深入探讨如何利用Ajax进行异步请求,特别是针对action的调用,并接收JSON(JavaScript Object Notation)数据,最后将这些数据展示在页面上。 首先,我们需要理解Ajax的基本工作原理。Ajax的核心是...
在請求來臨後,RequestProcessor 根據請求URI呼叫對應的Action物件,將工作交給它,並在最後由Action物件得到一個ActionForward物件, ActionServlet使用ActionForward得知將流程forward至指定的資源。 當請求到達...
本篇文章将深入探讨`jsp:forward`的使用方法以及在用户信息验证中的应用。 首先,`jsp:forward`的语法结构如下: ```jsp <jsp:forward page="targetPage.jsp" /> ``` 其中,`page`属性是必需的,它指定了要转发的...
4. **运行期添加参数**:在Action或JSP页面中,可以通过ActionForward动态添加请求参数,例如使用`<html:link>`标签或JSTL的`<c:url>`标签,将参数值附加到转发路径中。 **ActionMapping** 1. **ActionMapping属性...
当接收到一个请求时,`ActionServlet`会查找与请求路径匹配的`ActionMapping`,然后创建或重用对应的`ActionForm`对象,调用`Action`的`execute`方法,并根据执行结果选择合适的`ActionForward`跳转到相应的视图。...
`ActionForward`对象可以通过两种方式创建和使用: 1. **动态创建**:在`execute()`方法内部,开发者可以手动创建`ActionForward`实例,指定其逻辑名称、跳转路径以及是否进行重定向。例如,`return new Action...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 获取表单数据 LoginForm loginForm = (LoginForm) ...
Execute()从ActionForm对象中获取数据,完成业务逻辑,返回一个ActionForward对象,ActionServlet再把客户请求转发给ActionForward对象指定的jsp组件,ActionForward对象指定的jsp生成动态的网页,返回给客户。
1. ActionServlet:是Struts框架中的控制器,负责接收用户请求,调用Action的execute()方法处理请求,并根据返回的ActionForward对象进行页面跳转。 2. Action:是一个接口,具体的业务逻辑由实现了这个接口的...