Struts2 - 运用基本JavaBean和简单的Validation(从零开始学习Strust2_03)
来源:互联网 发布:二维数组的使用 编辑:程序博客网 时间:2024/06/05 03:12
开发环境:
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)
结果图:
web.xml跟HelloWorld相似,将welcome-list的内容改为了register.jsp
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>struts2_20120312_01</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>register.jsp</welcome-file></welcome-file-list></web-app>
然后实现一个用户注册信息的JavaBean
package com.zeph.struts2.bean;public class UserRegInfo {private String account;private String password;private String pwdValidate;private String realName;private String email;private String msn;private String qq;public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getPwdValidate() {return pwdValidate;}public void setPwdValidate(String pwdValidate) {this.pwdValidate = pwdValidate;}public String getRealName() {return realName;}public void setRealName(String realName) {this.realName = realName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getMsn() {return msn;}public void setMsn(String msn) {this.msn = msn;}public String getQq() {return qq;}public void setQq(String qq) {this.qq = qq;}@Overridepublic String toString() {return "Account:" + getAccount() + "Real Name:" + getRealName()+ "E-Mail:" + getEmail() + "MSN:" + getMsn() + "QQ:" + getQq();}}
在Action的实现类RegisterAction.java中使用这个JavaBean,并实现Validation函数。
package com.zeph.struts2.action;import com.opensymphony.xwork2.ActionSupport;import com.zeph.struts2.bean.UserRegInfo;public class RegisterAction extends ActionSupport {private static final long serialVersionUID = 7711471040010683683L;private UserRegInfo userRegInfoBean;public UserRegInfo getUserRegInfoBean() {return userRegInfoBean;}public void setUserRegInfoBean(UserRegInfo userRegInfoBean) {this.userRegInfoBean = userRegInfoBean;}public void validate() {if (userRegInfoBean.getAccount().length() == 0) {addFieldError("userRegInfoBean.account", "Account is required.");}if (userRegInfoBean.getEmail().length() == 0) {addFieldError("userRegInfoBean.email", "Email is required.");}if (userRegInfoBean.getRealName().length() == 0) {addFieldError("userRegInfoBean.realName", "Real Name is required.");}if (!userRegInfoBean.getPassword().equals(userRegInfoBean.getPwdValidate())) {addFieldError("userRegInfoBean.pwdValidate","Password input is inconsistent");}}@Overridepublic String execute() throws Exception {return SUCCESS;}}
struts.xml中添加了一个<result name="input">/register.jsp</result>,当验证出现不合法的情况,就会转到这个result
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" /><package name="default" extends="struts-default"><action name="RegisterAction" class="com.zeph.struts2.action.RegisterAction"><result name="success">/regSuccess.jsp</result><result name="input">/register.jsp</result></action></package></struts>
因为使用了JavaBean,所以在Jsp中使用XXX.xxx的方式
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib uri="/struts-tags" prefix="s"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Register</title></head><body><s:form action="RegisterAction"><s:textfield name="userRegInfoBean.account" label="Account"></s:textfield><s:password name="userRegInfoBean.password" label="Password"></s:password><s:password name="userRegInfoBean.pwdValidate"label="Input Password Again"></s:password><s:textfield name="userRegInfoBean.realName" label="Real Name"></s:textfield><s:textfield name="userRegInfoBean.email" label="E-Mail"></s:textfield><s:textfield name="userRegInfoBean.msn" label="MSN"></s:textfield><s:textfield name="userRegInfoBean.qq" label="QQ"></s:textfield><s:submit></s:submit></s:form></body></html>
regSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib uri="/struts-tags" prefix="s"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Register Success</title></head><body>account:<s:property value="userRegInfoBean.account" /><br> real name:<s:property value="userRegInfoBean.realName" /><br> e-mail:<s:property value="userRegInfoBean.email" /><br> MSN:<s:property value="userRegInfoBean.msn" /><br> QQ:<s:property value="userRegInfoBean.qq" /></body></html>
- Struts2 - 运用基本JavaBean和简单的Validation(从零开始学习Strust2_03)
- Struts2 - 使用XML实现简单Validation(从零开始学习Strust2_05)
- struts2-validation简单总结
- Struts2 - Struts2的拦截器Interceptor(从零开始学习Strust2_08)
- Struts2 - Struts2的文件下载(从零开始学习Strust2_10)
- struts2之01了解和简单的运用
- struts2学习笔记(2)——简单的输入验证以及标签库的运用
- Struts2学习笔记(13)-----JavaBean属性的领域对象
- 最简单的JavaBean学习
- jsp,javaBean,DAO之间的简单混合运用实例
- Struts2 - Controll Tag的使用(从零开始学习Strust2_06)
- struts2的validation框架
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互
- Struts2学习(四) validation验证
- 简单工厂模式的基本运用
- Struts2 - HelloWorld(从零开始学习Strust2_01)
- 内省和javaBean的学习
- WINDOWS下输入法中英文切换
- 原来写博客和记日记是不同的感觉,写博客要思考更加的仔细,因为有人在看
- Fedora16 安装相关
- hdu1813---IDA*
- C语言----函数的形参与实参
- Struts2 - 运用基本JavaBean和简单的Validation(从零开始学习Strust2_03)
- Java执行SQL脚本文件
- Android 2.3 SD卡挂载流程浅析(二)
- (转)Cannot make a static reference to the non-static method
- Symmedian Point
- jquery 做的导航
- sysfs文件系统学习
- MFC扫雷外挂的编程
- python3 cc dos脚本