fengda2870 2008年10月05日
struts2自定义抛出异常错误问题
因为上传文件比较慢
所以我将代码贴出来
目录结构

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>

...全文
548 点赞 收藏 13
写回复
13 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Web 开发
创建于2007-09-28

5.2w+

社区成员

34.1w+

社区内容

Java Web 开发
社区公告
暂无公告