我正在开发一个 web 应用程序,我们将代码和 web 资源(如 css、图像文件等)分开。
我有一个文件位于此处:
项目根目录/src/main/webapp/images/icons/16/add.png
在这个位置:
项目根目录/src/main/java/se/su/dsv/scipro/icons
我有这样的课:
package se.su.dsv.scipro.icons;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.model.Model;
public class ImageIcon extends Image {
private static final long serialVersionUID = 1L;
public static final String ICON_NEW_ADD = "../../../../../../webapp/images/icons/16/add.png";
public ImageIcon(String id, String name, String size, String alt){
super(id, new ResourceReference(ImageIcon.class, name));
if(alt != null){
add(new AttributeAppender("alt", true, new Model<String>(alt), " "));
}
else {
add(new AttributeAppender("alt", true, new Model<String>(name), " "));
}
}
public ImageIcon(String id, String name){
this(id, name, "", null);
}
public ImageIcon(String id, String name, String alt){
this(id, name, "", alt);
}
public static ResourceReference getImage(String imageName, String imageSize){
return new ResourceReference(ImageIcon.class, imageName);
}
}
但是当我在面板或页面的某个地方使用这个类时,例如这样:
add(new ImageIcon("new_add", ImageIcon.ICON_NEW_ADD));
我得到以下异常:
2011 Oct 18 11:11:17,947[ERROR] - RequestCycle - Can't instantiate page using constructor public se.su.dsv.scipro.basepages.DemoPage(org.apache.wicket.PageParameters) and argument
org.apache.wicket.WicketRuntimeException: Can't instantiate page using constructor public se.su.dsv.scipro.basepages.DemoPage(org.apache.wicket.PageParameters) and argument
at org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:212)
at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:65)
at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(BookmarkablePageRequestTarget.java:298)
at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.getPage(BookmarkablePageRequestTarget.java:320)
at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:234)
at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1436)
at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:486)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:319)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:188)
... 27 more
Caused by: java.lang.IllegalArgumentException: Invalid path ../../../../../../webapp/images/icons/16/add.png
at org.apache.wicket.util.lang.Packages.absolutePath(Packages.java:105)
at org.apache.wicket.util.lang.Packages.absolutePath(Packages.java:44)
at org.apache.wicket.markup.html.PackageResource.<init>(PackageResource.java:504)
at org.apache.wicket.markup.html.PackageResource.newPackageResource(PackageResource.java:448)
at org.apache.wicket.markup.html.PackageResource.get(PackageResource.java:424)
at org.apache.wicket.ResourceReference.newResource(ResourceReference.java:304)
at org.apache.wicket.ResourceReference.bind(ResourceReference.java:151)
at org.apache.wicket.markup.html.image.resource.LocalizedImageResource.bind(LocalizedImageResource.java:180)
at org.apache.wicket.markup.html.image.resource.LocalizedImageResource.setResourceReference(LocalizedImageResource.java:246)
at org.apache.wicket.markup.html.image.Image.setImageResourceReference(Image.java:178)
at org.apache.wicket.markup.html.image.Image.<init>(Image.java:101)
at org.apache.wicket.markup.html.image.Image.<init>(Image.java:77)
at se.su.dsv.scipro.icons.ImageIcon.<init>(ImageIcon.java:83)
at se.su.dsv.scipro.icons.ImageIcon.<init>(ImageIcon.java:93)
at se.su.dsv.scipro.basepages.DemoPage.<init>(DemoPage.java:16)
... 32 more
我猜这是因为图像文件位于任何 Java 包之外?我该如何解决这个问题?