目录:
- 表单的应用
- 创建Model 类
- 创建View 页面文件
- 创建Controller 类
- 测试演示
- 校验的应用
- 增加 validate() 方法
- 增加 input 跳转配置
- 校验演示
- 定义校验信息样式
[一]、表单的应用
1.创建Model 类:Person.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.micmiu.struts2.demo.register.model; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class Person { private String firstName; private String lastName; private String blogurl; private String email; private int age; public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getBlogurl() { return blogurl; } public String getEmail() { return email; } public int getAge() { return age; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setBlogurl(String blogurl) { this.blogurl = blogurl; } public void setEmail(String email) { this.email = email; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [firstName=" + firstName + ", lastName=" + lastName + ", blogurl=" + blogurl + ", email=" + email + ", age=" + age + "]"; } } |
2.创建View 页面文件
register.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome to micmiu.com !</title> </head> <body> <h3>Register for demo .</h3> <s:form action="register"> <s:textfield name="personBean.firstName" label="First name" /> <s:textfield name="personBean.lastName" label="Last name" /> <s:textfield name="personBean.blogurl" label="Blog" /> <s:textfield name="personBean.email" label="Email" /> <s:textfield name="personBean.age" label="Age" /> <s:submit /> </s:form> </body> </html> |
thankyou.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome to micmiu.com !</title> </head> <body> <h3>Thank you for registering .</h3> <p> Your registration information: <s:property value="personBean" /> </p> <p> <a href="<s:url action='index' />">Return to home page</a>. </p> </body> </html> |
3.创建Controller 类
Action 类:RegisterAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.micmiu.struts2.demo.register.action; import com.micmiu.struts2.demo.register.model.Person; import com.opensymphony.xwork2.ActionSupport; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class RegisterAction extends ActionSupport { private static final long serialVersionUID = 1L; private Person personBean; @Override public String execute() throws Exception { // call Service class to store personBean's state in database return SUCCESS; } public Person getPersonBean() { return personBean; } public void setPersonBean(Person person) { personBean = person; } } |
配置文件 struts.xml 中增加:
1 2 3 4 |
<action name="register" class="com.micmiu.struts2.demo.register.action.RegisterAction" method="execute"> <result name="success">/thankyou.jsp</result> </action> |
4.测试演示
启动项目,浏览器地址栏输入:http://localhost:8082/struts2-basic-demo/register.jsp 回车:
看到上述类似截图表示运行成功。
[二]、校验的应用
1.增加 validate() 方法
Controller 类:RegisterAction.java 添加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void validate() { if ("".equals(personBean.getFirstName())) { addFieldError("personBean.firstName", "First name is required."); } if ("".equals(personBean.getBlogurl())) { addFieldError("personBean.blogurl", "Blog is required."); } if ("".equals(personBean.getEmail())) { addFieldError("personBean.email", "Email is required."); } if (personBean.getAge() < 18) { addFieldError("personBean.age", "Age is required and must be 18 or older"); } |
2. 增加 input 跳转配置
配置文件 struts.xml 中 register 修改成如下:
1 2 3 4 5 |
<action name="register" class="com.micmiu.struts2.demo.register.action.RegisterAction" method="execute"> <result name="success">/thankyou.jsp</result> <result name="input">/register.jsp</result> </action> |
3. 校验演示
启动项目,浏览器地址栏输入:http://localhost:8082/struts2-basic-demo/register.jsp ,提交后验证校验:
看到上述类似截图表示运行成功。
4.定义校验信息样式
在页面HTML中的<head></head>之间增加标签 <s:head />即可,提交后验证校验:
看到上述类似截图表示运行成功。
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/j2ee/struts/struts2-form-validation/
0 条评论。