kisonlee
kisonlee
2009-08-27 21:09

在struts2的simple主题下,如何才能显示拦截错误信息呢

已采纳

在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"%>

function checkFile() { if(document.getElementById("upload1").value == "") { alert("请选择封面图片!"); return false; } else if(document.getElementById("upload2").value == "") { alert("请选择封底图片!"); return false; } else return true; }





上传教材图片







第三步:上传教材图片



/s:fielderror




/s:form







[/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">

function trim(s) { return s.replace(/^\s*/,"").replace(/\s*$/,""); } function validate() { var value; for(i = 1;i <= 13;i++) { value = trim(document.getElementById('item' + i).value); if(value == "" || value == null) { alert('请填写相关项!'); return false; } } return true; }






创建教材信息

第二步:创建教材信息
教材尺寸:
编著时间:
发行时间:



  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • walsh_bupt walsh_bupt 12年前

    [size=medium] 你把theme="simple"放到submit里边干嘛呢,我是说你把它放到form标签中,就像下边这样:

    [/size]

    点赞 评论 复制链接分享
  • walsh_bupt walsh_bupt 12年前

    首先说明一下,接收什么样的格式是你的配置文件中指定的,既然你的配置文件中只指定了jpg格式,按你传别的格式当然就不行了,

    你可以在validate里先做验证呀,把你的方法名改为:
    public void validate(),这个方法是在执行execute方法之间自动调用的,在这个首先判断你的文件后缀名,是否是jpg格式以及upload是否为空呀,如果你的upload本来就是空的,那你直接调用File[] files = getUpload(); 当然空指针异常了。就没有赋值成功呀。

    点赞 评论 复制链接分享
  • walsh_bupt walsh_bupt 12年前

    楼主,你的struts.xml中配置的文件类型太少了,改为下边的,试试吧:

    [code="xml"]

    image/bmp,image/PNG,image/gif,image/JPEG,image/jpg,image/pjpeg





    102400000

    [/code]

    另外楼主,可以参看一下文章,完整的上传文件的实例:

    [url]http://www.iteye.com/topic/270046[/url]

    点赞 评论 复制链接分享
  • walsh_bupt walsh_bupt 12年前

    [size=medium]theme属性值simple作用就是取消struts2的表格,同时也自动取消在个字段上的类型转换错误信息[/size]

    那你显示错误可以用用来显示ActionError级别来显示呀。

    在你的action中的public void validate()方法中,加入你想验证username,就可以这样写呀(你的Action要继承ActionSupport):
    [code="java"]if(null == username || username.length()10) {//校验用户名
    this.addActionError("用户名长度大于6小于10");
    }[/code]

    然后在你的JSP页面中,在你的form表单上面加上:
    [size=medium]<!-- 用来显示ActionError级别的错误 -->

    [/size]

    而在你的form表单中:
    [size=medium][/size]

    按照以上我给你写的,即可显示错误信息。同时你还可以把Struts2的表格去掉,你可以自己设置样式。

    点赞 评论 复制链接分享
  • walsh_bupt walsh_bupt 12年前

    [code="jsp"]


    用户注册




































    用户名:
    密码:
    请再次输入密码:
    年龄:
    出生年月:
    毕业院校:
    毕业时间:





    [/code]

    [size=medium]楼主只需要在你的JSP页面中加入下边这行代码即可。[/size]

    [code="java"]

    [/code]
    点赞 评论 复制链接分享
  • wanghaolovezlq wanghaolovezlq 12年前
    点赞 评论 复制链接分享

相关推荐