摘要:Struts2提供了安全的服务器验证方法,使用action中的validate方法即可
下面将展现简单的输入验证怎么写
1,表单index.jsp代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP ‘index.jsp‘ starting page</title> 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 </head> 23 24 <body> 25 <h1> 学生信息 </h1> 26 <s:form action="st" method="post"> 27 <s:textfield name="name" label="姓名"></s:textfield> 28 <s:textfield name="age" label="年龄"></s:textfield> 29 <s:textfield name="mark" label="分数"></s:textfield> 30 <s:textfield name="enrolltime" label="入学时间"></s:textfield> 31 <tr> 32 <td align="center" colspan="2"> 33 <input type="submit" value="提交"/> 34 <input type="reset" value="重置"/> 35 </td> 36 </tr> 37 </s:form> 38 39 </body> 40 </html>
2,编写Student类来接受表单参数,代码如下:
1 package pojo; 2 3 import java.util.Date; 4 5 public class Student { 6 private String name; 7 private int age; 8 private double mark; 9 private Date enrolltime; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public int getAge() { 17 return age; 18 } 19 public void setAge(int age) { 20 this.age = age; 21 } 22 public double getMark() { 23 return mark; 24 } 25 public void setMark(double mark) { 26 this.mark = mark; 27 } 28 public Date getEnrolltime() { 29 return enrolltime; 30 } 31 public void setEnrolltime(Date enrolltime) { 32 this.enrolltime = enrolltime; 33 } 34 35 36 37 }
3,Action代码如下:
1 package action; 2 3 import pojo.Student; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 import com.opensymphony.xwork2.ModelDriven; 7 8 public class StuAction extends ActionSupport implements ModelDriven<Student>{ 9 private Student student=new Student(); 10 @Override 11 public Student getModel() { 12 // TODO Auto-generated method stub 13 return student; 14 } 15 @Override 16 public String execute() throws Exception { 17 System.out.println(student.getName()); 18 System.out.println(student.getAge()); 19 System.out.println(student.getMark()); 20 System.out.println(student.getEnrolltime()); 21 return SUCCESS; 22 23 24 } 25 26 27 28 }
4,struts.xml配置如下:
1 <?xml version="1.0" encoding="GBK" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <package name="actions" extends="struts-default"> 5 <action name="st" class="action.StuAction"> 6 <result name="input">/index.jsp</result> 7 <result>/Success.jsp</result> 8 </action> 9 10 </package> 11 12 13 </struts>
注:name="input"是在Action模型装换异常或validate方法验证发生异常时自动转换页面,此处为index.jsp
5,Action正确执行后的Success.jsp代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP ‘Success.jsp‘ starting page</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 26 <body> 27 <h1>消息显示</h1> 28 <s:property value="name"/><br/> 29 <s:property value="age"/><br/> 30 <s:property value="mark"/><br/> 31 <s:property value="enrolltime"/><br/> 32 </body> 33 </html>
时间: 2024-10-07 02:34:21