h1>登录(validator框架客户端验证(javascript))</h1>
<hr>
<html:javascript formName="loginForm"/>
<html:form action="login2.do" onsubmit="return validateLoginForm(this)">
username : <html:text property="username"/><br/>
password : <html:password property="password"/><br>
<html:submit/>
</html:form>
25、声明式异常
Action中
UserManager.getInstance().login(username, password);
return mapping.findForward("success");
业务逻辑记得先建立两个异常类
public void login(String username, String password) {
// if (!"admin".equals(username)) {
// throw new UserNotFoundException();
// }
// if (!"admin".equals(password)) {
// throw new PasswordErrorException();
// }
// }
Struts配置文件中
定义全局异常
<global-exceptions>
<!-- 全局异常
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
-->
定义action
<action-mappings>
<action path="/login"
type="com.bjsxt.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
input="/login.jsp"<!-- 默认异常转向 -->
>
<!--
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
-->
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login.jsp"/>
</action>
Jsp页面
<html:errors/>
声明式异常传递一个站位附的办法
异常类中
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String msg) {
super(msg);
}
}
业务调用中
public void login(String username, String password) {
// if (!"admin".equals(username)) {
// throw new UserNotFoundException(username);
// }
// if (!"admin".equals(password)) {
// throw new PasswordErrorException();
// }
// }
26、个性化异常处理
建立资源文件
user.login.success={0},登录成功
user.not.found=用户不能找到,用户名称=[{0}]
user.password.error=密码错误
user.login.error=登录失败
建立一个异常处理类
public class ErrorCodeException extends RuntimeException {
private String errorCode;
private Object[] args;
//没有占位符
public ErrorCodeException(String errorCode) {
this(errorCode, null);
}
//有一个占位符的
public ErrorCodeException(String errorCode, Object args0) {
this(errorCode, new Object[]{args0});
}
//有多个占位符的
public ErrorCodeException(String errorCode, Object[] args) {
this.errorCode = errorCode;
this.args = args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
业务调用类
public void login(String username, String password) {
// if (!"admin".equals(username)) {
// throw new ErrorCodeException("user.not.found", username);
// }
// if (!"admin".equals(password)) {
// throw new ErrorCodeException("user.password.error");
// }
Struts配置文件
先建立一个继承handle 类复写里面的一点方法
public class ErrorCodeExceptionHandler extends ExceptionHandler {
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
//写下面的if方法
if (!(ex instanceof ErrorCodeException)) {
return super.execute(ex, ae, mapping, formInstance, request, response);
}
//
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
//修改下面
ErrorCodeException ece = (ErrorCodeException)ex;
String errorCode = ece.getErrorCode();
Object[] args = ece.getArgs();
error = new ActionMessage(errorCode, args);
property = error.getKey();
//
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
写struts配置文件的这里
全局异常
<exceptionkey="error.exception" type="com.bjsxt.struts.AppException" handler="com.bjsxt.struts.AppExceptionHandler"/>
27、不要个性化异常
建立一个异常类
public class AppException extends RuntimeException {
public AppException(String msg) {
super(msg);
}
业务调用
public void login(String username, String password) {
if (!"admin".equals(username)) {
throw new AppException("用户不能找到,用户=【" + username + "】");
}
if (!"admin".equals(password)) {
throw new AppException("密码不正确!");
}
Struts配置文件
<exceptionkey="error.exception" type="com.bjsxt.struts.AppException"/>
配置资源文件
error.exception={0}
没有必要建立一个异常处理类
28、forwardaction的使用
只要这样配置就能转向了不用配置action
<action path="/login1"
type="org.apache.struts.actions.ForwardAction"
parameter="/login.jsp"
></action>
29、Dispatchaction
页面转向window.self.location = "usermaint.do?command=find&userId=" + document.getElementsByName("selectFlag")[j].value;
采用DispathAction
* 如果覆写DispathAction中的execute方法,必须显示的用super调用execute方法
* parameter参数值不能是execute或perform
30、容错处理//可以去掉查找所有用户的方法
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("-----------UserAction.unspecified()-----------");
//调用业务逻辑操作
List userList = UserManager.getInstance().findAllUserList();
request.setAttribute("userlist", userList);
return mapping.findForward("list_success");
// ActionForward af = new ActionForward();
// af.setPath("/user/usermaint.do?command=list");
// af.setRedirect(true);
// return af;
}
31、模式匹配
建立三个页面
Add—success.jsp
Del-success.jsp
Modify-success.jsp
Jdp页面中
<a href="user/add.do?command=add">添加用户</a><br>
<a href="user/del.do?command=del">删除用户</a><br>
<a href="user/modify.do?command=modify">修改用户</a><br>
Struts配置
<action path="/user/*"
type="com.bjsxt.struts.UserAction"
parameter="command"
>
<!--
<forward name="success" path="/user/{1}_success.jsp"/>
-->
<forward name="success" path="/{0}_success.jsp"/>
</action>
Action中
return mapping.findForward("success");
33、补充说明
要把所有的jsp放到web-inf下浏览器是不能访问得到的,只有通过Action才能访问得到
注意图片的现实只与basename有关系也目录无关系
../img
<action path="/basedata/additem"与下面无关
<forwardname="upload_detail" path="/WEB-INF/jsp/basedata/item_upload.jsp"/>
验证用户存在与否
建立一个基本的action,然后其他action继承它就可以了!
public class BaseAction extends DispatchAction {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (request.getSession().getAttribute("user") == null) {
return mapping.findForward("index");
}
return super.execute(mapping, form, request, response);
}
其他action
public class ItemAction extends BaseAction {
注意返回的设置要带上返回的差数
否则用过滤类来保存
或用threadlocal做传递
function goBack() {
window.self.location = "item.do?command=list&pageNo=${itemForm.pageNo}&pageSize=${itemForm.pageSize}";
}
下拉选择列表的写法
<td><select name="unitId" class="select1" id="unitId">
<c:forEach items="${drp:getItemUnitList()}" var="iu" >
<c:set var="select" value=""/>
<c:if test="${ iu.id eq item.unit.id }">
<c:set var="select" value="selected"/>
</c:if>
<option value="${iu.id}" ${ select }>${iu.name}</option>
</c:forEach>
</select></td>
分页补充
function resetItem() {
document.getElementsByName("clientIdOrName").value = "";
}
function topPage() {
window.self.location = "item.do?command=list&pageNo=${pagemodel.topPageNo}&pageSize=${itemForm.pageSize}&clientIdOrName=${ itemForm.clientIdOrName }"
}
function previousPage() {
window.self.location = "item.do?command=list&pageNo=${pagemodel.previousPageNo}&pageSize=${itemForm.pageSize}&clientIdOrName=${ itemForm.clientIdOrName }"
}
function nextPage() {
window.self.location = "item.do?command=list&pageNo=${pagemodel.nextPageNo}&pageSize=${itemForm.pageSize}&clientIdOrName=${ itemForm.clientIdOrName }"
}
function bottomPage() {
window.self.location = "item.do?command=list&pageNo=${pagemodel.bottomPageNo}&pageSize=${itemForm.pageSize}&clientIdOrName=${ itemForm.clientIdOrName }"
}
按键事件的设置
function myOnkeypress() {
if (window.event.keyCode == 13) {
queryItem();
}
单元格中
<td width="46%"><input name="clientIdOrName" type="text" class="text1" id="clientIdOrName" value="${ itemForm.clientIdOrName }" size="50" maxlength="50" onkeypress="myOnkeypress()">
分享到:
相关推荐
Struts1.x是一款经典的Java Web开发框架,由Apache软件基金会维护,主要用于构建MVC(Model-View-Controller)架构的应用程序。本系列教程是针对初学者的全面指导,旨在帮助学习者快速掌握Struts1.x的核心概念和使用...
Struts1.x是一个经典的Java Web开发框架,它遵循Model-View-Controller(MVC)设计模式,用于构建可维护性和可扩展性高的企业级应用程序。在这个"Struts1.x_Project.zip"压缩包中,我们可以看到一个使用Struts1.x...
Struts1.x是Apache软件基金会旗下Jakarta项目的一个核心组件,它是一款开源的MVC(Model-View-Controller)框架,用于构建企业级Java Web应用程序。这个框架在2000年代初非常流行,因其规范化的开发流程和强大的功能...
Struts1.x是Apache软件基金会开发的一个开源框架,主要用于构建基于Java的企业级Web应用程序。它在MVC(Model-View-Controller)设计模式的基础上提供了一种实现方式,使得开发者能够更方便地处理业务逻辑、视图展示...
Struts1.x是Apache软件基金会的一个开源项目,它是一个基于MVC(Model-View-Controller)设计模式的Java Web应用程序框架。在Web开发领域,Struts1.x曾是广泛使用的框架,它帮助开发者构建结构清晰、易于维护的Web...
`Struts 1.x` 提供了内置的表单验证机制,允许开发者在 `ActionForm` 类中定义校验规则,便于在客户端和服务器端进行数据验证。 ### 8. 整合其他技术 `Struts 1.x` 可以很好地与其他开源技术集成,如Hibernate...
Struts2.x提供了丰富的拦截器(Interceptor),这些拦截器可以对Action执行前后的流程进行定制,比如日志记录、权限验证、数据校验等。拦截器链使得业务逻辑的处理更加灵活和模块化。 配置方面,Struts2.x使用XML...
Struts1.x_Tags是Apache Struts框架的一个重要组成部分,它是用于构建MVC(Model-View-Controller)架构Web应用程序的标签库。Struts1.x_Tags提供了丰富的JSP标签,使得开发者能够更加方便地创建动态用户界面,提高...
Struts1.x是一个经典的Java Web框架,用于构建MVC(模型-视图-控制器)架构的应用程序。在本文中,我们将深入探讨如何配置Struts1.x,并通过一个包含登录功能和基本CRUD(创建、读取、更新、删除)操作的实例来说明...
Struts2提供了一套完整的验证机制,可以通过在Action类中定义校验规则或者在struts.xml中配置校验规则来检查文件大小、类型等。如果上传过程中出现错误,可以通过返回特定的结果代码来跳转到错误页面。 7. **文件...
Struts 2.3.x系列在功能上提供了很多改进,如增强的类型转换、更好的注解支持、更丰富的拦截器链等。此外,安全方面也是重点关注的,因为它历史上曾出现过一些知名的漏洞,如CVE-2017-5638(Struts2 S2-045漏洞),...
SSH(Spring、Struts2.x 和 Hibernate)是一个经典的Java企业级开发框架组合,常用于构建高效、可维护的Web应用程序。这个登录实例是基于这些框架的集成应用,结合了MySQL数据库和Tomcat服务器,提供了用户登录和...
5. **Struts2与Struts1.x的区别**:Struts2并非Struts1.x的简单升级,而是基于Webwork构建的新框架,核心机制有很大的变化。Struts2的动作类不再直接继承自Action,而是继承自com.opensymphony.xwork2.ActionSupport...
在Struts 1.x版本中,拦截器(Interceptor)是实现业务逻辑与展现逻辑解耦合的关键工具。拦截器在请求处理流程中扮演了重要角色,它允许我们在Action执行前后插入自定义的逻辑,如日志、权限检查、数据校验等。 ...
3. **Struts2.x标签库**:介绍Struts2.x提供的标签,如`s:form`标签用于创建表单,`s:property`标签用于显示对象属性,`s:action`标签用于执行Action,以及各种数据校验和国际化标签。 4. **TLD注册与使用**:解释...
10.为什么要使用struts2代替struts1.x 7 二、struts.xml配置及例程 7 1.配置文件的优先级 7 2.配置形式 8 3.package配置相关 8 4.分工合作include:指定多个配置文件 10 5.tomcat认证访问 10 6.初识拦截器 11 7....
在"struts_auto_Validation"的包中,我们很显然关注的是Struts 1.x版本中的自动验证机制。 自动验证是Struts 1.x中的一项重要特性,它简化了表单数据验证的流程。当你创建一个Struts应用时,通常会定义ActionForm...
一个第三方扩展--Struts TestCase--提供了一套Structs1.x的模拟对象(来进行测试)。 Structs2.0 Action可以通过初始化、设置属性、调用方法来测试,“依赖注入”支持也使测试更容易。 捕获输入 Structs1.x...
**Jakarta Struts 1.x 框架详解** Jakarta Struts 是一款经典的Java Web应用程序框架,由Apache软件基金会的Jakarta项目开发并维护。它在2000年代初期广泛流行,为构建MVC(Model-View-Controller)架构的Web应用...
Struts2框架是基于Struts1的升级,它采用了拦截器模式来处理请求,提供了一种更灵活和可扩展的架构。Struts2提供了强大的数据校验、结果类型、国际化和异常处理机制,使得开发者可以更加专注于业务逻辑而不是底层...