2 回答
TA贡献1895条经验 获得超7个赞
您需要什么:
“ /”处的DefaultServlet(推荐,这是servlet规范的要求)
这应该在“默认”的命名调度程序处(servlet规范的另一个要求)
通过init-params配置的具有自定义静态内容的备用DefaultServlet
使用与“默认”不同的命名调度程序(以避免其他servlet规范功能之间的名称冲突)
您可以使用ServletHolder.setInitParameter(name,value)来完成此操作
确保将pathInfoOnly参数设置为true(以解决“默认”命名调度程序的特殊情况)
您自己的Servlet,用于提供动态内容。
AltDefaultServlet.java
package jetty.demo;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class AltDefaultServlet
{
public static void main(String[] args)
{
System.setProperty("org.eclipse.jetty.LEVEL","INFO");
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
// The filesystem paths we will map
String homePath = System.getProperty("user.home");
String pwdPath = System.getProperty("user.dir");
// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(Resource.newResource(pwdPath));
context.setContextPath("/");
server.setHandler(context);
// add a simple Servlet at "/dynamic/*"
ServletHolder holderDynamic = new ServletHolder("dynamic", DynamicServlet.class);
context.addServlet(holderDynamic, "/dynamic/*");
// add special pathspec of "/home/" content mapped to the homePath
ServletHolder holderHome = new ServletHolder("static-home", DefaultServlet.class);
holderHome.setInitParameter("resourceBase",homePath);
holderHome.setInitParameter("dirAllowed","true");
holderHome.setInitParameter("pathInfoOnly","true");
context.addServlet(holderHome,"/home/*");
// Lastly, the default servlet for root content (always needed, to satisfy servlet spec)
// It is important that this is last.
ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
holderPwd.setInitParameter("dirAllowed","true");
context.addServlet(holderPwd,"/");
try
{
server.start();
server.dump(System.err);
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
}
}
TA贡献1752条经验 获得超4个赞
我正在通过DefaultServlet将angularjs用作静态内容,默认的'/ home /'路由一切正常,但是未找到'/ home / *'重定向到码头页面而不是angularjs内容
添加回答
举报