异常类 针对java的
package com.qxm.excepton;
public class DefineException extends Exception {
private String exception = "";
public DefineException(String exception)
{
this.exception = exception;
}
public String toString()
{
return "This is a " + exception;
}
}
package com.qxm.excepton;
public class FileBigger extends Exception {
private int count;
public FileBigger(int count){
this.count = count;
}
public String toString(){
return "你上传的文件的大小为"+count;
}
}
package com.qxm.struts.action;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.qxm.excepton.FileBigger;
import com.qxm.struts.form.UpLoadForm;
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws FileBigger{
UpLoadForm upLoadForm = (UpLoadForm) form;
FormFile ff = upLoadForm.getFile();
String fileName = ff.getFileName();
String fileType = ff.getContentType();
int fileSize = ff.getFileSize();
System.out.println("fileName: "+fileName);
System.out.println("fileType: "+fileType);
System.out.println("fileSize: "+fileSize);
if(fileSize>=1024*1024*2){
try {
throw new FileBigger(fileSize);
} catch (FileBigger e) {
e.printStackTrace();
System.out.println("文件过大了");
return mapping.findForward("failure");
}
}
try {
byte[] out = ff.getFileData();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\"+fileName));
bos.write(out);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
}
package com.qxm.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.qxm.excepton.DefineException;
import com.qxm.struts.form.LoginForm;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
if(username.equals("abc")){
throw new DefineException(password);
}
} catch (DefineException e) {
e.printStackTrace();
return new ActionForward("/error.jsp");
}
return mapping.findForward("success");
}
}
file
package com.qxm.struts.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UpLoadForm extends ActionForm {
private static final long serialVersionUID = -1487216025710423329L;
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
package com.qxm.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends ActionForm {
private String username;
private String password;
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(username.equals("") || username == null){
errors.add("username",new ActionMessage("login.error.username"));
}
else
if(password.equals("") || password == null){
errors.add("password",new ActionMessage("login.error.password"));
}
else
if(username.length()>=6)
errors.add("username",new ActionMessage("login.error.username.length"));
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = null;
this.password = null;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
# Resources for parameter 'com.qxm.struts.ApplicationResources'
# Project struts_exception
##This is Login
login.error.username="Your username is not null"
login.error.password="Your password is not null"
login.error.username.length="You username's length is less than 7"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.qxm.struts.form.LoginForm" />
<form-bean name="upLoadForm" type="com.qxm.struts.form.UpLoadForm" />
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
name="loginForm"
path="/login"
scope="request"
type="com.qxm.struts.action.LoginAction"
input="/login.jsp"
validate="true"
>
<exception
key="login.error"
path="/error.jsp"
type="com.qxm.excepton.DefineException" />
<forward name="success" path="/loginsuccess.jsp" />
</action>
<action
attribute="upLoadForm"
name="upLoadForm"
path="/upLoad"
scope="request"
type="com.qxm.struts.action.UpLoadAction">
<exception key="upload.filebiggest.defineexception" type="com.qxm.excepton.FileBigger"/>
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/error.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.qxm.struts.ApplicationResources" />
</struts-config>
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UpLoadForm form</title>
</head>
<body>
<html:form action="/upLoad" enctype="multipart/form-data" method="post">
file : <html:file property="file"/><html:errors property="file"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
package com.qxm.excepton;
public class DefineException extends Exception {
private String exception = "";
public DefineException(String exception)
{
this.exception = exception;
}
public String toString()
{
return "This is a " + exception;
}
}
package com.qxm.excepton;
public class FileBigger extends Exception {
private int count;
public FileBigger(int count){
this.count = count;
}
public String toString(){
return "你上传的文件的大小为"+count;
}
}
package com.qxm.struts.action;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.qxm.excepton.FileBigger;
import com.qxm.struts.form.UpLoadForm;
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws FileBigger{
UpLoadForm upLoadForm = (UpLoadForm) form;
FormFile ff = upLoadForm.getFile();
String fileName = ff.getFileName();
String fileType = ff.getContentType();
int fileSize = ff.getFileSize();
System.out.println("fileName: "+fileName);
System.out.println("fileType: "+fileType);
System.out.println("fileSize: "+fileSize);
if(fileSize>=1024*1024*2){
try {
throw new FileBigger(fileSize);
} catch (FileBigger e) {
e.printStackTrace();
System.out.println("文件过大了");
return mapping.findForward("failure");
}
}
try {
byte[] out = ff.getFileData();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\"+fileName));
bos.write(out);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
}
package com.qxm.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.qxm.excepton.DefineException;
import com.qxm.struts.form.LoginForm;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
if(username.equals("abc")){
throw new DefineException(password);
}
} catch (DefineException e) {
e.printStackTrace();
return new ActionForward("/error.jsp");
}
return mapping.findForward("success");
}
}
file
package com.qxm.struts.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UpLoadForm extends ActionForm {
private static final long serialVersionUID = -1487216025710423329L;
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
package com.qxm.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends ActionForm {
private String username;
private String password;
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(username.equals("") || username == null){
errors.add("username",new ActionMessage("login.error.username"));
}
else
if(password.equals("") || password == null){
errors.add("password",new ActionMessage("login.error.password"));
}
else
if(username.length()>=6)
errors.add("username",new ActionMessage("login.error.username.length"));
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = null;
this.password = null;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
# Resources for parameter 'com.qxm.struts.ApplicationResources'
# Project struts_exception
##This is Login
login.error.username="Your username is not null"
login.error.password="Your password is not null"
login.error.username.length="You username's length is less than 7"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.qxm.struts.form.LoginForm" />
<form-bean name="upLoadForm" type="com.qxm.struts.form.UpLoadForm" />
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
name="loginForm"
path="/login"
scope="request"
type="com.qxm.struts.action.LoginAction"
input="/login.jsp"
validate="true"
>
<exception
key="login.error"
path="/error.jsp"
type="com.qxm.excepton.DefineException" />
<forward name="success" path="/loginsuccess.jsp" />
</action>
<action
attribute="upLoadForm"
name="upLoadForm"
path="/upLoad"
scope="request"
type="com.qxm.struts.action.UpLoadAction">
<exception key="upload.filebiggest.defineexception" type="com.qxm.excepton.FileBigger"/>
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/error.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.qxm.struts.ApplicationResources" />
</struts-config>
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UpLoadForm form</title>
</head>
<body>
<html:form action="/upLoad" enctype="multipart/form-data" method="post">
file : <html:file property="file"/><html:errors property="file"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
Struts框架异常处理
7570

被折叠的 条评论
为什么被折叠?



