州:
问题
当Struts action servlet初始化时,它会专门进行以下调用:
InputStream input = getServletContext()。getResourceAsStream(“/ WEB-INF / web.xml”);
导致:
javax.servlet.ServletException: The /WEB-INF/web.xml was not found.
at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1781)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:349)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:432)
at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:260)
问题:
我想这是因为struts在请求资源时使用了前导斜杠。
信息:
这是我用来启动码头和两个依赖类的类。
public class JettyRunner {
public static void main(String[] args) throws Exception {
if (args == null) {
args = new String[0];
}
// Construct the new arguments for jetty-runner
boolean transientState = false;
int port = 8070;
Server server = new Server(port);
WebAppContext webapp = new WebAppContext(".", "");
webapp.setConfigurations(new Configuration[]{new JspConfiguration()});
ClassPathResourceHandler resourceHandler = new ClassPathResourceHandler();
resourceHandler.setContextPath("");
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.addHandler(resourceHandler);
contexts.addHandler(webapp);
server.setHandler(contexts);
//server.setHandler(webapp);
URL jettyXmlURL = new JettyRunner().getClass().getClassLoader().getResource("jetty.xml");
XmlConfiguration configuration = new XmlConfiguration(jettyXmlURL); //or use new XmlConfiguration(new FileInputStream("myJetty.xml"));
//XmlConfiguration configuration = new XmlConfiguration(new FileInputStream("jetty.xml"));
configuration.configure(server);
server.start();
server.join();
}
}
public class JspConfiguration extends WebXmlConfiguration {
@Override
public Resource findWebXml(WebAppContext webAppContext) throws IOException, MalformedURLException {
URL path = getClass().getClassLoader().getResource("WEB-INF/web.xml");
return Resource.newResource(path);
}
}
public class ClassPathResourceHandler extends ContextHandler {
Logger log = Logger.getLogger(startupServlet.class.getName());
private ResourceHandler realResourceHandler = null;
public ClassPathResourceHandler() {
realResourceHandler = new ResourceHandlerImplementation();
}
@Override
public void doHandle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
realResourceHandler.handle(s, request, httpServletRequest, httpServletResponse);
}
private class ResourceHandlerImplementation extends ResourceHandler {
@Override
protected Resource getResource(HttpServletRequest httpServletRequest) throws MalformedURLException {
String requestedFile = httpServletRequest.getRequestURI();
log.debug("getResource(): " + requestedFile);
URL path = getClass().getResource(requestedFile);
try {
Resource resource = Resource.newResource(path);
if (resource != null && resource.exists() && !resource.isDirectory()) {
return resource;
}
else {
return null;
}
}
catch (IOException e) {
return null;
}
}
}
}
答案 0 :(得分:0)
Struts旨在作为Web应用程序运行,而不是作为独立应用程序运行。在JettyRunner
课程中,您可以使用main
方法启动Jetty。这不适用于Struts。如果要使用Struts 1.x,则必须创建Web应用程序。
答案 1 :(得分:0)
我能够使用WebAppContext的setWar()
方法为我的struts 1.3应用程序解决这个问题。我的基本码头跑步者的版本如下:
public class JettyRunner {
private static final Logger logger = Logger.getLogger(JettyRunner.class);
public static void main(String[] args) throws Exception {
WebAppContext context = new WebAppContext();
context.setWar(System.getProperty("user.dir") + "/src/main/webapp/");
context.setContextPath("/");
logger.debug(context.dump());
Server server = new Server(8080);
server.setHandler(context);
server.start();
server.join();
}
}