表单校验---struts2
生活随笔
收集整理的這篇文章主要介紹了
表单校验---struts2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 ? 寫validate方伐
1 讓你的Action繼承ActionSupport類,
重寫validate方法,(或validateXxxx)
判斷,寫入錯誤信息
jsp中用標簽顯示,struts。xml中配置轉向的頁面
package he.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;public class Login extends ActionSupport{String name;String pwd;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}@Overridepublic void validate() {// TODO Auto-generated method stubsuper.validate();if("".equals(name)||name==null){this.addFieldError("name", "name can not be null");}if("".equals(pwd)||pwd==null){this.addFieldError("pwd", "pwd can not be null");}}public String execute(){System.out.print(name+" \n"+pwd);if("scott".equals(name)&&"1234".equals(pwd)){Map<String, Object> session = ActionContext.getContext().getSession();session.put("user", name);return "success";}this.addActionError("name do not match pwd");return "fail";}}?
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name="struts2-demo1" extends="struts-default"><action name="login" class="he.action.Login"><result name='success' type='dispatcher'>/ok.jsp</result><result name='fail' type='dispatcher'>/index.jsp</result><result name="input">/index.jsp</result></action></package> </struts> Struts.xml <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib uri="/struts-tags" prefix="s" %> <!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><s:actionerror/><s:form action="login" ><s:textfield name="name" label="name" labelposition="left"></s:textfield><s:fielderror fieldName="name"></s:fielderror><br><s:textfield name="pwd" label="pwd" labelposition="left"></s:textfield><s:fielderror fieldName="pwd"></s:fielderror><br><s:submit value="OK"></s:submit></s:form></body> </html> index.jsp?
轉載于:https://www.cnblogs.com/hzzhero/p/5083658.html
總結
以上是生活随笔為你收集整理的表单校验---struts2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用代理下载Unity AssetSto
- 下一篇: CentOS 6.5 安装Python