Struts消息/错误信息传递机制实例

1.在src的tool包下建立资源文件及其对应的中文资源文件

   ApplicationResources.properties

  

   ApplicationResources_zh_CN.properties

 

 

2.在struts-config.xml中配置消息资源文件

  加入

   <message-resources parameter="tools.ApplicationResources" />

3.使用【可以使用message和error两种】在action文件中使用

 

  引入

  import org.apache.struts.action.ActionMessage;
  import org.apache.struts.action.ActionMessages;

  A 消息【message】/错误【error】

     使用:

      ActionMessages msg = new ActionMessages(); //新建

      //添加,相当于一个映射,在显示页面可根据关键词读取相应的内容

      //false代表不去查找资源文件中的对应项,直接显示消息内容

      msg.add("error",new ActionMessage("ERROR项登录失败",false)); 
      msg.add("info",new ActionMessage("INFO项登录失败",false)); 
     

     //直接显示,不用根据关键词取出内容,
      msg.add(ActionMessages.GLOBAL_MESSAGE,
                               new ActionMessage("直接显示 登录失败!!!!!",false)); 

     

      //到资源文件中查找error.login.failed的对应项

      //根据刚建立的资源文件,可知对应的内容为  【登录失败:用户名与密码无效!】
      msg.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("error.login.failed",true)); 
      //将消息加入request域

      this.addMessages(request,ams); //与error添加的唯一区别

      //this.addErrors(request,errorMessages);//区别

    

在转发到的页面显示:message.jsp

【message="true"代表显示的是message,若要显示error则不带此项即可,默认的】

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
 <title>
  消息页面
 </title>
</head>

<link href="images/myStyleClass.css" rel="stylesheet" type="text/css">

<body>

 <center>
  <h2>
   <a href="javascript:history.go(-1)">
    返回上一页
   </a>
  </h2>
 </center>
<hr>
  消息MESSAGE:<br>

  显示所有的消息:<br>
     <html:messages id="message" message="true">
          <bean:write name="message"/>
                <br />
     </html:messages>
<hr>

 显示对应关键字的消息:<br>
     <html:messages id="message" message="true" property="info">
       <bean:write name="message"/>
       <br />
      </html:messages> 
</body>
</html:html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值