struts2自定义抛出异常错误问题

fengda2870 2008-10-05 08:55:46
因为上传文件比较慢
所以我将代码贴出来
目录结构

PasswordException.java

package exception;

public class PasswordException extends Exception {

private static final long serialVersionUID = 8194779832109312113L;
private String message;

public PasswordException(String message) {
super( message );
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}



}


UsernameException.java源代码
package exception;

public class UsernameException extends Exception {

private static final long serialVersionUID = 5926837471651227661L;
private String message;

public UsernameException(String message) {
super( message );
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


Login.java源代码

package test;

import com.opensymphony.xwork2.ActionSupport;

import exception.PasswordException;
import exception.UsernameException;

public class Login extends ActionSupport{

private static final long serialVersionUID = 1L;
private String username;
private String password;

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;
}

public String execute () throws Exception {
if ( !"hello".equals(this.getUsername().trim()) ){
throw new UsernameException( "username" );
} else if ( !"world".equals(this.getPassword().trim()) ){
throw new PasswordException( "password" );
}else{
return SUCCESS;
}

// if ( "hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())){
// return "success";
// } else {
// this.addFieldError( "username" , "用户名或密码错误,请重新输入。" );
// this.setUsername( "" );
// this.setPassword( "" );
// return "failer";
// }
}

// public void validate() {
// if ( (null == this.getUsername()) || ("".equals(this.getUsername().trim())) ){
// this.addFieldError( "username" , "用户名不能为空。" );
// }
// if ( (null == this.getPassword()) || ("".equals(this.getPassword().trim())) ){
// this.addFieldError( "password" , "密码不能为空。" );
// }
// }

}

sturts.xml源代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<constant name="struts.custom.i18n.resources" value="message">
</constant>

<package name="struts2" extends="struts-default">
<action name="login" class="test.Login">
<exception-mapping result="username"
exception="test.UsernameException">
</exception-mapping>
<result name="username">/username.jsp</result>

<exception-mapping result="password"
exception="test.PasswordException">
</exception-mapping>
<result name="password">/password.jsp</result>

<result name="success">/result.jsp</result>
</action>
</package>
</struts>


web.xml源代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


login2.jsp源代码(没有JSP标签,选择了JScript源代码标签)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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:form action="login">
<s:textfield name="username" label="用户名:"></s:textfield>
<s:password name="password" label="密码:"></s:password>
<s:submit ></s:submit>
</s:form>
</body>
</html>

...全文
617 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuolan6266 2009-11-27
  • 打赏
  • 举报
回复
还是没有得到我想要的答案
liuliangdongyajie 2009-09-29
  • 打赏
  • 举报
回复
我也碰到这问题了,感觉是Struts2的bug,不能跳到错误页面去,无法捕捉自定义异常!!!垃圾,都一年了,还没有高手解决!!!网上找不到好的方法
javaindex 2008-11-13
  • 打赏
  • 举报
回复
<exception-mapping result="username"
exception="test.UsernameException">
</exception-mapping>
<result name="username">/username.jsp</result>

<exception-mapping result="password"
exception="test.PasswordException">
</exception-mapping>
你的配置错了,上面的exception应该为:exception="exception.UsernameException">

luke888 2008-11-08
  • 打赏
  • 举报
回复
我也遇到同样的问题,请问lz是怎么解决的?
fengda2870 2008-10-07
  • 打赏
  • 举报
回复
我的目的是想将异常捕获
然后在新的页面显示

<s:property value="exception" />

但是我运行的结果都是Tomcat报出的错误
lichenq 2008-10-07
  • 打赏
  • 举报
回复
javax.servlet.ServletException: exception.UsernameException: username

在输入框输入的username不是hello,抛出UsernameException,信息是“username”,很正常啊

如果你要截获异常,在页面显示的话,我不知道struts2怎么做
我只知道在struts1里面,你要在MessageResources.properties文件里面写上exception的id
struts才能找到这个异常的信息
fengda2870 2008-10-07
  • 打赏
  • 举报
回复
如果 this.getUsername()==null
那么 !"hello".equals(this.getUsername().trim() 这个表达式为真
怎么哪里错了?
还有如果this.getUsername()!=null
依旧抛出异常啊
wmj2003 2008-10-07
  • 打赏
  • 举报
回复
if ( !"hello".equals(this.getUsername().trim()) ){
很可能是this.getUsername()=null引起的。
睿音 2008-10-06
  • 打赏
  • 举报
回复
不太明白你的目的是什么哦~~~

我觉得你的现象很正常啊。你人工的抛出一个Exception,然后让struts捕获啊。


if ( !"hello".equals(this.getUsername().trim()) ){
throw new UsernameException( "username" ); //这里抛出异常后后面不再执行,而struts捕获的就是你这里抛出的这个异常错误
} else if ( !"world".equals(this.getPassword().trim()) ){
throw new PasswordException( "password" );
}else{
return SUCCESS;
}

  • 打赏
  • 举报
回复
我认为throw new UsernameException( "username" ); 这里出现错误。
应该改为:throw new UsernameException(username); 把引号去掉就行了,
因为username在xml文件中已经声明为test.UsernameException
fengda2870 2008-10-06
  • 打赏
  • 举报
回复
我是看浪曦的视频教程做的
throw new UsernameException( "username" );
这句代码后面返回值啊
直接抛出的
不知道哪里不对
sfli_g 2008-10-06
  • 打赏
  • 举报
回复
我没做过你这样的异常处理,不知道对还是不对

你抛出异常后不用给返回值了么?
这throw new UsernameException( "username" ); 后面
fengda2870 2008-10-05
  • 打赏
  • 举报
回复
抛出了如下异常:
运行之后,却抛出了异常
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: exception.UsernameException: username
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)


root cause

exception.UsernameException: username
test.Login.execute(Login.java:29)
……………………
……………………

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

找了很久
就是找不到哪里出的错误

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧