专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Exception

struts2抛自定义异常错误问题

发布时间:2010-06-05 12:36:49 文章来源:www.iduyao.cn 采编人员:星星草

我将代码贴出来
目录结构
自定义异常:
PasswordException.java
 

Java code


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源代码
 

Java code
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源代码
 

Java code


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 code


<?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 code


<?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>



	    
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: