在struts2的simple主题下,如何才能显示拦截错误信息呢,我不太想用xhtml,这样我的样式只能按它固定的了。。。。
[b]问题补充:[/b]
关键我jsp使用struts标签写的。。。。。
[b]问题补充:[/b]
另外问大家个问题,我上传图片,当非指定格式和大小时,jsp就挂了
upload.jsp
[code="java"]<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
上传教材图片
[/code]
Spread.java
[code="java"]package action;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
import bean.DBUtil;
import com.opensymphony.xwork2.ActionSupport;
public class Spread extends ActionSupport
{
private static final long serialVersionUID = 1L;
public File[] getUpload()
{
return upload;
}
public void setUpload(File[] upload)
{
this.upload = upload;
}
public String[] getUploadContentType()
{
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName()
{
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public String getSavePath()
{
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath)
{
this.savePath = savePath;
}
public void validateThirdStep()
{
clearErrorsAndMessages();
File [] files = getUpload();
BufferedImage image = null;
for (int i = 0; i < files.length; i++)
{
try
{
image = ImageIO.read(files[i]);
} catch(IOException e)
{
e.printStackTrace();
}
if (image.getWidth() > 260 || image.getHeight() > 260)
{
addFieldError("picSizeErr", "上传图片宽高不能超过260*260");
break;
}
}
}
public String excute() throws Exception
{
String imageFileName;
File[] files = getUpload();
for (int i = 0 ; i < files.length ; i++)
{
imageFileName = new Date().getTime() + getUploadFileName()[i].substring(getUploadFileName()[i].lastIndexOf("."));
realPath[i] = getSavePath() + "\\" + imageFileName;
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = new FileOutputStream(getSavePath() + "\\" + imageFileName);
fis = new FileInputStream(files[i]);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
try
{
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
fis.close();
fos.close();
}
}
return SUCCESS;
}
private static final int BUFFER_SIZE = 16 * 1024;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private String savePath;
private String [] realPath = new String[2];
}
[/code]
struts.xml
[code="java"]<?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">
/spread/spread1.jsp
/login.jsp
<package name="authority" extends="struts-default">
<interceptors>
<interceptor name="authority" class="interceptor.AuthorityInterceptor" />
</interceptors>
<default-interceptor-ref name="authority" />
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<action name="ThirdStep" class="action.Spread">
<result name="success">/spread/spread4.jsp </result>
<result name="input">/spread/upload.jsp </result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/pjpeg</param>
<param name="maximumSize">204800</param>
</interceptor-ref>
<param name="savePath">/upload</param>
</action>
</package>
[/code]
异常信息jsp:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
action.Spread.thirdStep(Spread.java:285)--(这句for (int i = 0 ; i < files.length ; i++))
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:33)
[b]问题补充:[/b]
我这个只能是JPG的,大小不能超过200KB,按要求可以正常上传,可是如果非jpg或者大于200KB,拦截器可以发现,但是jsp页面就挂了,不知道为啥,控制台不显示异常信息
[b]问题补充:[/b]
walsh
按你说的,如果在submit中添加simple,到时可以和reset一行,可是两个按键全部跑到真个table的最上面边了,这是个问题,你可以看看
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.util.*" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
创建教材信息
编著时间: | |
发行时间: |