`

Strrts2基于Annotation的输入验证

阅读更多

客户端jsp请求Action代码 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'login.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">
	
	<script type="text/javascript">
		function abc(id){
			var buttonObj = document.getElementById(id);
			buttonObj.style.color="red";
		}
		function efg(id){
			var buttonObj = document.getElementById(id);
			buttonObj.style.color="black";
		}
	</script>
	
  </head>
  
  <body>
  	<s:debug></s:debug>
  	
    <form action="login.action" method="post">
    	<table align="center">
    		<caption><h2>用户登录</h2></caption>
    		
    		<tr>
    			<td style="font-style: inherit;color: green">用户名:<input type="text" name="username" style="color: red;background: #fffddd" /><s:property value="errors.username"/></td>
    		</tr>
			<tr>
				<td style="font-style: inherit;color: green">密&nbsp;&nbsp;码:<input type="password" name="password" style="color: red;background: #fffddd"/></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input id="1" onmouseover="javascript:abc(this.id)" onmouseout="javascript:efg(this.id)" style="color: black" type="submit" value="登录"/>
				<input type="reset" id="2" onmouseover="javascript:abc(this.id)" onmouseout="javascript:efg(this.id)" style="color: black" value="重填" /></td>
			</tr>
    	</table>
    </form>
  </body>
</html>
 

Action处理,这里是关键 
package com.lbx.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@SuppressWarnings({ "deprecation", "serial" })
@Validation
public class LoginAction extends ActionSupport{
	
	private String username;
	private String password;
	
	//封装处理结果的tip属性
	private String tip;
	
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String getUsername() {
		return username;
	}
	@RequiredStringValidator(type=ValidatorType.FIELD,key="用户名必须写",message=" ")
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	@RequiredStringValidator(type=ValidatorType.FIELD,key="密码必须写",message=" ")
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String execute() throws Exception {
		if(this.getUsername().equals("libinxuan")){
			ActionContext.getContext().getSession().put("user", this.getUsername());
			this.setTip("欢迎," + this.getUsername() + ",你已经登录了!!");
			return SUCCESS;
		}
		this.setTip("登录失败,请重新再登录");
		return ERROR;
	}
	
}
 

struts.xml的配置
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts是Struts 2配置文件的根元素 -->
<struts>
	
	<package name="login" namespace="/" extends="struts-default">
		<action name="login" class="com.lbx.action.LoginAction">
			<result name="input">/login.jsp</result>
			<result>/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		
	</package>

</struts>
 

 

分享到:
评论

相关推荐

    基于Annotation的s2sh整合配置实现分页功能

    基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能

    Spring MVC Annotation验证的方法

    Spring MVC Annotation验证方法 Spring MVC 框架提供了多种验证方法,其中一种常用的方式是使用Annotation验证。本文将详细介绍 Spring MVC Annotation验证的方法,包括使用 Spring MVC 自带的 Annotation 验证和...

    基于annotation 的ssh整合(1)

    在本文中,我们将探讨如何基于注解(Annotation)进行SSH(Struts、Hibernate、Spring)的整合。传统的SSH整合通常依赖于XML配置文件,但随着技术的发展,注解的引入使得我们可以更加简洁地管理类和它们之间的关系,...

    基于annotation的aop实现

    基于Annotation的AOP实现是Spring框架的一个重要特性,它极大地简化了AOP的使用。在本篇文章中,我们将深入探讨基于Annotation的AOP实现,特别是动态代理的理念。 首先,了解什么是AOP。AOP的核心概念是“切面”...

    S2SH整合基于annotation

    本主题聚焦于"S2SH"的整合,即Struts2、Hibernate3和Spring2的结合,并特别强调了使用Annotation的方式。以下是对这些技术以及整合过程的详尽解析。 **Struts2** 是一个基于MVC(Model-View-Controller)设计模式的...

    基于annotation s2sh实现零配置的CRM

    【基于Annotation的S2SH实现零配置CRM】 在IT行业中,CRM(Customer Relationship Management)系统是一种用于管理企业与客户之间关系的重要工具。S2SH,即Struts2、Spring和Hibernate的组合,是Java Web开发中常用...

    基于Annotation的Java单元测试框架.pdf

    "基于Annotation的Java单元测试框架" 本文主要介绍了基于Annotation的Java单元测试框架,讨论了Annotation在Java EE中的应用、反射技术的使用和JUnit单元测试框架的结合,建立了一个自动化单元测试框架结构。 一、...

    使用struts2的annotation验证

    博文链接:https://flym.iteye.com/blog/174358

    extjs+s2sh基于annotation的理财系统

    【标题】"extjs+s2sh基于annotation的理财系统"是一个综合性的IT项目,它结合了前端的ExtJS框架和后端的Struts2、Spring、Hibernate(S2SH)框架,并利用注解(Annotation)进行配置,构建了一个完整的理财管理系统...

    基于Annotation的Servlet配置

    然而,随着Java技术的发展,基于Annotation的Servlet配置逐渐成为主流,因为它提供了更加简洁、直观的编程模型。这篇博文"基于Annotation的Servlet配置"探讨了如何使用注解来配置Servlet,让我们深入理解这一现代Web...

    基于Annotation的MVC框架SSH示例.rar

    在这个"基于Annotation的MVC框架SSH示例"中,我们将探讨如何使用注解(Annotation)来简化SSH框架的配置和实现。 1. **Spring框架**:Spring是核心容器,负责管理应用对象的生命周期和依赖注入。在传统的Spring配置...

    基于annotation 的ssh整合(2)

    ### 基于Annotation的SSH整合开发:Spring、Struts与Hibernate协同工作 #### 概述 本文将探讨如何在项目中实现基于Annotation的SSH(Spring、Struts、Hibernate)框架整合。随着技术的发展,XML配置逐渐被...

    基于annotation s2sh实现零配置的教务管理系统

    【标题】"基于annotation s2sh实现零配置的教务管理系统"揭示了这个项目的核心技术特点,即使用了Spring、Struts2和Hibernate这三大框架的集成(S2SH),并借助注解(Annotation)来实现系统的零配置。在传统的S2SH...

    Extjs+s2sh基于annotation实现的酒店管理系统

    【标题】:“Extjs+s2sh基于annotation实现的酒店管理系统”是一个综合性的IT项目,它融合了前端的Extjs框架和后端的Spring、Struts2以及Hibernate这三大经典技术(合称为S2SH),并利用注解(Annotation)进行简化...

    基于Annotation并对DAO层封装具有分页功能的S2SH整合实例

    在这个基于Annotation并对DAO层封装具有分页功能的S2SH整合实例中,我们将探讨如何利用这些技术的特性来提高开发效率和代码可维护性。 首先,让我们深入了解一下Struts2。Struts2是基于MVC模式的开源Web应用框架,...

    SSH2增删改查的项目基于Annotation

    在这个基于Annotation的SSH2增删改查项目中,我们将深入探讨这三个框架如何协同工作,以及如何利用注解简化开发过程。 1. **Struts2**: Struts2是MVC(Model-View-Controller)设计模式的实现,负责处理HTTP请求...

Global site tag (gtag.js) - Google Analytics