标签:blog http io os ar 使用 java for sp
解决struts2中validation.xml配置无效的问题,我使用了xml的验证,却始终发现无法生效,后面发现才是xml的头文件的格式问题,修改了一下就好了。
成功的xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message>You must enter a value for bar.</message> </field-validator> </field> </validators>
这句话的格式要正确,否则就无法生效了。
如何利用validation.xml进行验证的步骤如下:
1、写一个ValideAction.java,也就是处理业务逻辑的类
package com.test.action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.test.model.Contact; import com.test.model.User; public class ValideAction extends ActionSupport implements ModelDriven<User>{ /** * */ private static final long serialVersionUID = 1L; private User user; private String username; private Contact contact; public Contact getContact() { return contact; } public void setContact(Contact contact) { this.contact = contact; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String password; public String execute() { //System.out.println(user.getContact().getPhone()); return SUCCESS; } @Override public User getModel() { // TODO Auto-generated method stub if (null == user) { return user = new User(); } return user; } }
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message>You must enter a value for bar.</message> </field-validator> </field> </validators>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!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>Insert title here</title> </head> <body> <!-- 这个地方是用来显示错误信息 --> <s:fielderror/> <form action="data.action" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> -------------------联系方式----------------<br> 手机:<input type="text" name="contact.phone"><br> 地址:<input type="text" name="contact.address"><br> 邮箱:<input type="text" name="contact.email"><br> <input type="submit" name="ok"><br> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 采用注解的方式 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="" extends="struts-default"> <!-- 正确和错误的分别跳转,input是在验证失败后会跳转的页面 --> <action name="data" class="com.test.action.ValideAction"> <result name="success">/result.jsp</result> <result name="input">/login.jsp</result> </action> </package> <!-- Add packages here --> </struts>
5、最终显示JSP页面就是上面那个文件,主要用<s:fielderror/>来显示错误信息
6、文件目录结构
输出结果
解决struts2中validation.xml配置无效的问题
标签:blog http io os ar 使用 java for sp
原文地址:http://blog.csdn.net/itbuluoge/article/details/40483509