首页 > 其他分享 >关于struts中的ActionError和ActionMessage的占位符{0}的使用

关于struts中的ActionError和ActionMessage的占位符{0}的使用

时间:2023-06-08 20:32:51浏览次数:40  
标签:username ActionErrors ActionMessage form struts ActionError HelloForm public



先说说ActionError,

   一般的情况下我们会自己写的ActionForm的validate方法来对提交过来的表单做验证

  开始的时候我也是在想这个仅仅是return 了一个ActionErrors

  这里是我的form源码

package com.little.struts.form;
public class HelloForm extends ActionForm {
 private String username;
 public String getUsername() {
  return username;
 }
 public ActionErrors validate(ActionMapping mapping,//这里就是我们做验证的
   HttpServletRequest request) {
  ActionErrors errors=new ActionErrors();
  if(username==null||username.length()<1){
   errors.add("error", new ActionMessage("test"));//就是我的国际化文件中的key
  }
  System.out.println("ActionErrors validate");
  return errors;
 }
 public void setUsername(String username) {
  this.username = username;
 }
}

下面我的国际化文件ApplicationResource.properties

usernull=welcome to java World !{0}//这个{0}是表示一个占位符的,以便我们以后来动态地改变
test=username is null

下面是我的Action的源码

package com.little.struts.action;
import com.little.struts.form.HelloForm;
public class HelloAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  HelloForm helloForm = (HelloForm) form;
  ActionMessages msgs=new ActionMessages();//创建一个消息的类似如容器的东东
  ActionMessage msg=new ActionMessage("usernull",helloForm.getUsername());//真正的消息
                                                                        //usernull就是国际化文件中的一个key
  //helloForm.getUsername()就填充了国际化文件中的{0}
   msgs.add("name", msg);                                  //添加消息到容器,
                                                                        //我发现name是可以随便取名的
  this.saveMessages(request, msgs);                //这个实际上就是把消息容器放到request中的Attribute里 
  return mapping.findForward("hello"); 
 }
}

这样我们就只剩下如何在jsp页面中读取消息了

<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
 
<html>
 <body>
     <html:messages id="msg" message="true">              //message的属性默认的是false也就是错误的信息
     <bean:write name="msg"/>                                      
     </html:messages>
     <html:errors/>                                                       //这里当然是输出错误信息啦
  <html:form action="/hello">                                           
   username : <html:text property="username"/>          //当然这里的form也可写成html的形式
   <html:submit/>
  </html:form>
 </body>
</html>

讲到了这里是不是发现有一个疑问,error发生的时候是如何实现跳转的???

下面请看我的struts-config.xml

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="helloForm" type="com.little.struts.form.HelloForm" />
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="helloForm"
      input="/form/hello.jsp"                         //这里就是当错误发生的时候跳转路径
      name="helloForm"
      path="/hello"
      scope="request"
      type="com.little.struts.action.HelloAction" >
      <forward name="hello" path="/form/hello.jsp"></forward>
      </action>
  </action-mappings>
  <message-resources parameter="com.little.struts.ApplicationResources" />//国际化文件配置
</struts-config>

 

总结;

当error发生的时候,struts也像是一个中断一样,直接的跳转

标签:username,ActionErrors,ActionMessage,form,struts,ActionError,HelloForm,public
From: https://blog.51cto.com/u_16065168/6443065

相关文章

  • struts的ActionErrors和ActionMessage写法
    我在Action中的ActionErrors的实例中add了一系列的ActionError, 在JSP中用<html:errors/>显示,但什么都没有! 是不是ActionErrors.add("abc", ActionError)中的第一个字符串“abc”要添加在什么资源文件里呀?怎么建立资源文件,怎么添加?怎么显示? 我非常迷惑,请各位赐教,谢谢!   A......
  • struts2,Jboss
                    ......
  • Struts2实例
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitio......
  • 使用JPA+Struts2+Spring 在 google Appengine开发应用
    本文同时发表在我在googleAppengine上的搭建的博客:[url]http://blogfor11lu.appspot.com/articleaction_view.action?article.id=agtibG9nZm9yMTFsdXIPCxIHQXJ0aWNsZRjBtQMM[/url]之前用JDO和Struts2在googleAppengine上试着写了一个简单的blog程序,但我还是希望使用Spring......
  • 渗透测试-struts2攻防环境搭建拿shell
    一、下载Jspstudy打开目录D:\JspStudy\tomcat\webapps二、打开struts2并进行拿shell1.打开struts2在浏览器中输入网址http://localhost:8080/struts2-showcase/showcase.action点击上面的Configuration,点击ActionChaining点击上面的Configuration,点击ActionChaining点击......
  • struts2 jpg文件上传失败 Content-Type not allowed 解决方案
    org.apache.struts2.interceptor.FileUploadInterceptor-Content-Typenotallowed:upload"upload__75549ca5_1239ddc5ce6__8000_00000161.tmp"image/pjpeg原有配置:用ext+struts2上传时一直显示进度条不消失:Java代码<actionname="xxxAction"cla......
  • Struts2----中使用ValueStack、ActionContext、ServletContext、request、session等
     声明:本文参考Struts2版本为2.3.1.2,内容仅供参考,限于笔者水平有限,难免有所疏漏,望您能友善指出。本文发表于ITEYE,谢绝转载。1.ValueStack  ValueStack在中文版的《Struts2深入浅出》一书中译作“值栈”。其本身数据结构是一个栈,使用者可以把一些对象(又称作bean)存入值栈中,然后......
  • struts2+Spring+Hibernate+Jquery (json)
          Struts2文件的上传和下载http://wing929.javaeye.com/blog/216118基于struts+spring+hibernate+jquery的jmesa的分页实现样例http://wing929.javaeye.com/blog/216292http://hi.baidu.com/angel_lishiwen/blog/item/cf266627fcb59327d4074288.html stru......
  • Struts2+extjs (Json)
            web开发过程中,经常需要使用Ajax进行数据的交互。ExtJs表单提交默认使用异步的方式,若使用Struts2的Action进行数据的处理,则需要返回一个Json数据客户端才会收到服务器的处理结果,我们使用jsonplugin插件从Action中返回结果给用户,下面我们就来实现这一方法: 1、下......
  • struts2 + extjs
     extjs整合struts2:要导入的包很重要,很多demo就是没有说清楚这个,导致无法安装他们的博文进行演示。那么接下去,就简单了。struts.xml的配置Xml代码1.<?xmlversion="1.0"encoding="UTF-8"?>2.<!DOCTYPEstrutsPUBLIC3."-//ApacheSoftwareFoundation//D......