首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

问个struts2错误处理有关问题

2012-03-06 
问个struts2异常处理问题!用了struts2异常处理后发现。处理国际化反倒不是很容易.比如struts1定义一个异常,

问个struts2异常处理问题!
用了struts2异常处理后发现。处理国际化反倒不是很容易.

比如struts1定义一个异常,那里抛出都能定位到显示界面。而且能够使用资源的key值。

到struts2反倒实现异常国际化比较难,也可能我不知道好方法。

目前一共看了两个方案,都不是很好。

第一个就是异常在action中抛出,那么就可以用getText()方法了。但是这个太受限了,很多异常不需要在action中使用。但是要抛出。

throw new MyException(getText(资源key值,参数));---但是getText方法只能在action中用或是页面,而不能在DAO层直接使用。

第二个就是自己写拦截器,拦截自己定义的异常,只要是项目定义的异常就抛出。方式固然好,但是麻烦。

public class ProExceptionInterceptor extends AbstractInterceptor {

  public String intercept(ActionInvocation invocation) throws Exception {
  String result = null;
  try {
  result = invocation.invoke();
  } catch (项目自定义的那个根异常 exception) {
   
  ActionSupport as = (ActionSupport)invocation.getAction();
  将异常压入action,这个过程还是用getText方法。
  如果是链式异常
  List exceptions = exception.getExceptions();
  if (exceptions != null && !exceptions.isEmpty()) {
  for (int i = 0; i < exceptions.size(); i++) {
  项目自定义的那个根异常 subEX = (项目自定义的那个根异常) exceptions.get(i);  
  将异常压入action;
  }
  }
  throw exception;
  }
  return result;
  }


这两个方式都不是很好,肯定有更好的方式,但是一时半刻还真没办法,我希望达到 这样一个效果,无论异常在那里抛出都能实现国际化。



[解决办法]
sf
[解决办法]
楼主是强人,我帮顶吧。
[解决办法]


确实不方便



每个公司都有自己的一套处理机制
[解决办法]
我坚持struts1不放松。
[解决办法]
我想LZ 是会自己写个拦截器的吧, 那还是写一个吧
看看http://darkranger.javaeye.com/ 他的BLOG 上有拦截器的,是不是有好的拦截器设置啊??
[解决办法]
搞不清楚,帮顶
[解决办法]
值得研究。。。。

[解决办法]
帮顶!!!!
[解决办法]
学习
[解决办法]
在s2里面建议使用拦截器来处理吧,不过如果当前异常不需要在前台明细显示或者做重定向页面提示,一般情况下直接try {} catch{}掉就行,毕竟异常的处理机制还是需要谨慎,处理不好,麻烦很多
[解决办法]
发不了贴,在此问个问题,希望各位解答,谢谢

我用struts2 做添加消息功能 出现问题:
<form id="inputForm" action="${ctx}/message/message!save.action" method="post">
<input type="hidden" name="id" value="${message.id}" />
<table class="inputView">

<tr>
<td width="85">消息标题:</td>
<td width="381"><input type="text" name="title" size="40"
value="${message.title}" /><span class="STYLE1">*</span></td>
</tr>
<tr>
<td>&nbsp;&nbsp;作&nbsp;&nbsp;&nbsp; 者:</td>
<td><input type="text" name="loginName" size="40"
value="${user.loginName}" /><span class="STYLE1">*</span></td>
</tr>
<tr>
<td>消息类别:</td>
<td><span id="postTypeDiv"> <select name="ptid" id="ptid">


<c:if test="${message == null}">
<option value="0">----请选择----</option>
</c:if>
<c:if test="${message!=null}">
<option value="${message.catalog.id}">${message.catalog.type}</option>
</c:if>
<s:iterator value="catalogs">
<option value="${id}">${type}</option>
</s:iterator>
</select> </span> <input type="button" value="添加" onclick=" textPostTypeShow()" /> <input
type="button" value="修改" onclick="update_PostType()" /> <input
type="button" value="删除" onclick="delete_PostType()" />

<div id="textPostType" style="display: none;"><input type="text"
id="typeName" /> <input type="hidden" id="typeId" /> <input
type="button" value="保存" onclick="input_postType()" /> <input
type="button" value="取消" onclick="textPostTypeHide()" /></div>
</td>
</tr>
<tr>
<td>消息内容:</td>
<td><label> <textarea name="content"
style="width: 300px; height: 100px">${message.content}</textarea> </label></td>
</tr>

<tr>
<td colspan="2" align="center"><span style="color: red;">注:
* 的必须填写</span></td>
</tr>
<tr>

<td colspan="2" align="center">
<input type="image" name="submit" id="submit" src="${ctx }/images/sch/yes.gif" style="cursor: pointer;" />
&nbsp; <img src="${ctx }/images/sch/no.gif" style="cursor: pointer;"onclick="history.back()" />
</td>
</tr>
</table>

</form>
以上是我的页面的部分代码:
然后我再MessageAction中:接收的属性:// 页面属性
private long id;
private long ptid;
private String title;
private Catalog catalog;//消息类型类
private List<Catalog> catalogs;
private String content;
private String createDate;
private Message message;//消息类
private User user;//登陆的用户
private String loginName;//登陆用户名
private int state;//消息的状态,0未看,1看了

消息message类:private User user;// 发消息的人
private String title;// 消息标题
private String content;// 消息内容
private Date createDate;// 消息创建时间
private Date lastModifyDate;//修改的时间
private Catalog catalog;//消息的类型
private int state;//消息的状态
private int isDelete;
但是我在添加页面中保存时:ognl.MethodFailedException: Method "setId" failed for object cms.web.message.MessageAction@190dc79 [java.lang.NoSuchMethodException: cms.web.message.MessageAction.setId([Ljava.lang.String;)] 
 actionForm中属性都加了get和set,但是报以上的错,我检查好久不知所措,希望各位高手解答,谢谢


[解决办法]
rt

热点排行