Я работаю над веб-приложением, в котором мы держим код и веб-ресурсы (такие как CSS, файлы изображений и т. Д.) Отдельно.
У меня есть файл, расположенный здесь:
root проекта /src / main / webapp / images / icons / 16 / add.png
И в этом месте:
root проекта / 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));
Iполучить следующее исключение:
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?Как я могу решить это?