struts2 이상 처리 (전역 및 국부 이상 정의)
1.exception.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
<s:form action="exception.action" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
2.result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
username:${requestScope.username }
</body>
</html>
3.usernameException.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
usernameException
</body>
</html>
4.UsernameException.java(이상 클래스 정의)
package com.hitsoft.exception;
public class UsernameException extends Exception{
private String message;
public UsernameException(String message){
super(message);
this.message = message;
}
@Override
public String getMessage() {
return super.getMessage();
}
public void setMessage(String message) {
this.message = message;
}
}
5.ExceptionAction.java
package com.hitsoft.action;
import com.hitsoft.exception.UsernameException;
import com.opensymphony.xwork2.ActionSupport;
public class ExceptionAction extends ActionSupport{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception{
if(!"hello".equals(username)){
throw new UsernameException("username invalid!");
}else{
return "success";
}
}
}
6.struts.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>
<package name="struts2" extends="struts-default">
<!-- -->
<global-results>
<result name="usernameInvalid" type="redirect">/usernameException.jsp</result>
</global-results>
<action name="exception" class="com.hitsoft.action.ExceptionAction">
<!-- -->
<exception-mapping result="usernameInvalid" exception="com.hitsoft.exception.UsernameException"></exception-mapping>
<result name="success">/result.jsp</result>
<result name="input">/exception.jsp</result>
</action>
</package>
</struts>
2.struts2 전역 이상 처리
1.exception.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
<s:form action="exception.action" method="post">
<s:textfield name="password" label="password"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
2.result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
password:${requestScope.password }
</body>
</html>
3.passwordException.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
passwordException
</body>
</html>
4.PasswordException.java(이상 클래스 정의)
package com.hitsoft.exception;
public class PasswordException extends Exception{
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;
}
}
5.ExceptionAction.java
package com.hitsoft.action;
import com.hitsoft.exception.PasswordException;
import com.opensymphony.xwork2.ActionSupport;
public class ExceptionAction extends ActionSupport{
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception{
if(!"world".equals(password)){
throw new PasswordException("password invalid!");
}else{
return "success";
}
}
}
6.struts.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>
<package name="struts2" extends="struts-default">
<!-- -->
<global-results>
<result name="passwordInvalid" type="redirect">/passwordException.jsp</result>
</global-results>
<!-- -->
<global-exception-mappings>
<exception-mapping result="passwordInvalid" exception="com.hitsoft.exception.PasswordException"></exception-mapping>
</global-exception-mappings>
<action name="exception" class="com.hitsoft.action.ExceptionAction">
<result name="success">/result.jsp</result>
<result name="input">/exception.jsp</result>
</action>
</package>
</struts>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception Class에서 에러 코드 해석 ~초기초편~직장에서 C# 프로젝트가 내뿜는 오류 코드를 구문 분석하고 오류의 위치를 확인하기 위해 Exception class를 활용할 수 있었습니다. 지금까지 Exception Class 에 대해서 별로 파악할 수 없었기 때...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.