读书人

Strrts2基于Annotation的输入证验

发布时间: 2012-09-20 09:36:50 作者: rapoo

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">密  码:<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" })@Validationpublic 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;}@Overridepublic 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>
?

?

读书人网 >软件架构设计

热点推荐