一、struts2参数传递错误

1. 基本数据类型的传递最好使用包装类,原因是struts 2.1之后使用基本数据类型如果参数为空会报错
2. 日期参数的传递最好定义一个区域的属性(定义locale),在struts.properties里面定义struts.locale=zh_CN,原因是日期在不同的区域中格式不一样,所以默认情况下日期会随着浏览器的不同使用不同的区域的日期格式

二、struts2的输入错误验证

1. 在前一篇文章关于struts2系列(一)的文章中搭建的struts2的开发环境中改写HelloWorldAction.java,在里面复写ActionSupport的validate()方法,然后在这个方法里面使用ActionSupport的addFieldError()方法添加错误

HelloWorldAction.java:

 package com.study.struts2.demo;

 import com.opensymphony.xwork2.ActionSupport;

 public class HelloWorldAction extends ActionSupport {

     /**
*
*/
private static final long serialVersionUID = 6522825387249101847L; private String msg; @Override
public void validate() {
if (msg == null || "".equals(msg)) {
super.addFieldError("msg", "信息不能为空"); }
} @Override
public String execute() throws Exception { // 通过request取得jsp的内置对象
// this.msg="hellowrold123";
return SUCCESS;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

2. 在jindex.jsp页面通过struts2的标签<s:property value="fieldErrors['msg'][0]" />获取错误显示

 <%@ page language="java" pageEncoding="UTF-8"%>
<!--引入struts2的标签 -->
<%@ taglib prefix="s" uri="/struts-tags"%>
<body>
<p>
<a href="<s:url action='hello'/>">Hello World</a>
</p> <!--struts2的取值标签 value="<s:property value="msg" />"输入正确的被保留 -->
<form action="hello.action">
信息:<input type="text" name="msg" value="<s:property value="msg" />" />
<s:property value="fieldErrors['msg'][0]" />
<br> <input type="submit" value="提交">
</form> <!--验证通过后显示 -->
<h2>
<s:property value="msg" />
</h2>
</body>
</html>

3. 验证:在浏览器输入地址http://localhost:8080/TestStruts2/

3.1 首次进入页面

3.2  什么都不输入,点击提交按钮

3.3 输入内容再点击提交按钮

struts2系列(二):struts2参数传递错误、struts2的输入错误验证的更多相关文章

  1. 【SSH框架】之Struts2系列(二)

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联 1.Struts2常量配置 (1).Struts2默认常量配置文件路径,如下图: (2).Strut ...

  2. struts2官方 中文教程 系列二:Hello World项目

    先贴个本帖的地址,免得其它网站被爬去了struts2入门系列二之Hello World  即 http://www.cnblogs.com/linghaoxinpian/p/6898779.html ...

  3. struts2讲义----二

    Struts的namespace 示例工程Struts2_0200_Namespace Struts.xml <struts> <constant name="struts ...

  4. Java框架之Struts2(二)

    一.Action 配置说明 //请求的直接转发 <package name="packageUser" namespace="" extends=&quo ...

  5. Struts2系列漏洞起始篇

    前言 到目前位置struts2的漏洞编号已经到了S2-057,一直想系统的学习下Struts2的漏洞,但由于工作量较大,一直搁浅.最近由于工作需要,借此机会来填下坑.个人认为一个框架漏洞出来了仅仅看下 ...

  6. struts2总结二:第一个简单的struts2程序

    到struts2官网上面下载struts2的jar包,然后解压. struts2的入门程序,实现简单的用户登录,struts2里面的helllo world.利用eclipse的开发工作如下: 1.首 ...

  7. 二十六:Struts2 和 spring整合

    二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...

  8. 二十四、Struts2中的UI标签

    二十四.Struts2中的UI标签 Struts2中UI标签的优势: 数据回显 页面布局和排版(Freemark),struts2提供了一些常用的排版(主题:xhtml默认 simple ajax) ...

  9. Struts2系列笔记(7)---Struts2类型转换

    Struts2类型转换      struts2中内置了大量的类型转换器用来完成数据类型转换的问题,这篇随笔主要通过两个方面来写Struts类型转换 1:Struts2内置的类型转换器 2:如何自定义 ...

随机推荐

  1. vue 的事件冒泡

    一.事件冒泡 方法一.使用event.cancelBubble = true来组织冒泡 <div @click="show2()"> <input type=&q ...

  2. 【转】MATLAB conv2函数的理解

    另附:http://blog.csdn.net/anan1205/article/details/12313593 原文:http://blog.csdn.net/andrewseu/article/ ...

  3. tf.truncated_normal

    tf.truncated_normal truncated_normal( shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name ...

  4. 移动web开发(二)——viewport

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scal ...

  5. Eigen教程(2)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...

  6. BeautifulSoup 使用select方法详解(通过标签名,类名, id,组合,属性查找)

    import requestsfrom bs4 import BeautifulSoup blslib="html5lib"user_agent="Mozilla/5.0 ...

  7. [Python] 探索性编程与idleX

    1 探索式编程 前面介绍过几个Python的Interactive Shell,对于Python, Ruby这类动态语言来说,一个交互式的shell(用 emacs的术语来说,叫做REPL,也就是Re ...

  8. win8中完成进度

    public sealed partial class WorkItem : Page { private IAsyncAction _threadPoolWorkItem; private Manu ...

  9. android studio connot resolve

    使用AS的时候,肯定会遇到这个问题. 我使用清除缓存都不能解决,不过无意中发现代码变了. final Button select = (Button) findViewById(android.R.i ...

  10. [转]mysql 行转列 列转行

    原文地址:http://www.cnblogs.com/xiaoxi/p/7151433.html 一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABL ...