[英]Page not found. Spring MVC + Jetty
I'm trying to start up Jetty with Spring MVC that contains jsp and @Controller. 我正在尝试使用包含jsp和@Controller的Spring MVC启动Jetty。 After start jetty I've got every time WARN if I try to call any pages. 在开始码头后,如果我尝试拨打任何页面,我每次都有WARN。 For example 例如
ttp://localhost:8080/getMessageStats/ TTP://本地主机:8080 / getMessageStats /
No mapping found for HTTP request with URI [/resources/css/bootstrap.min.css] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-57ad85ed' No mapping found for HTTP request with URI [/resources/js/bootstrap.min.js] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-57ad85ed' 在DispatcherServlet中找不到带有URI [/resources/css/bootstrap.min.css]的HTTP请求的映射,名称为'org.springframework.web.servlet.DispatcherServlet-57ad85ed'找不到带URI的HTTP请求的映射[/ resources / js DispatcherServlet中的/bootstrap.min.js],名称为“org.springframework.web.servlet.DispatcherServlet-57ad85ed”
web.xml web.xml中
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/cloud/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
mvc-dispatcher-servlet.xml MVC-调度-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="net.cloud.web.statistics"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
WebServer.class WebServer.class
public class WebServer {
private static final Logger log = LoggerFactory.getLogger(WebServer.class);
private static final String CONTEXT_PATH = "/";
private Server httpServer;
@Value("${http_statistics_port}")
private Integer port;
@Value("${start_page_path}")
private String startPagePath;
public void init() {
try {
httpServer = new Server(port);
httpServer.setHandler(getServletContextHandler());
httpServer.setStopAtShutdown(true);
httpServer.start();
log.info("WebServer start ...");
httpServer.join();
} catch (Exception ex) {
log.error("Exception in http server. Exception: {}", ex.getMessage());
}
}
private static ServletContextHandler getServletContextHandler() throws IOException {
WebAppContext contextHandler = new WebAppContext();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath(CONTEXT_PATH);
WebApplicationContext context = getContext();
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), CONTEXT_PATH);
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
contextHandler.setDescriptor("/webapp/WEB-INF/web.xml");
return contextHandler;
}
private static WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("webapp/WEB-INF/");
log.info("CONTEXT name {} locations {}", context.getDisplayName(), context.getConfigLocations());
return context;
}
} }
GetMessagesStatsController.class GetMessagesStatsController.class
@Controller
@RequestMapping("/getMessageStats")
public class GetMessageStatsController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
GetMessageStatsRPC rpc = new GetMessageStatsRPC();
model.addAttribute("result", rpc.getResult());
return "getMessageStats";
}
@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody
String generateJsonResponse() {
GetMessageStatsRPC rpc = new GetMessageStatsRPC();
return rpc.getResult().toString();
}
}
Project tree (Sorry, no rating to upload picture). 项目树(对不起,没有评级上传图片)。 Classes: 类别:
src/java/main/net/.../WebServer.class;src/java/main/net/.../GetMessagesStatsController.class src/main/webapp/WEB-INF/web.xml; src / java / main / net /.../ WebServer.class; src / java / main / net /.../ GetMessagesStatsController.class src / main / webapp / WEB-INF / web.xml; src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml; 的src /主/ web应用/ WEB-INF / MVC-调度-servlet.xml中; src/main/webapp/WEB-INF/pages/*.jsp; SRC /主/ web应用/ WEB-INF /页/ * JSP。 src/main/webapp/resources/css and src/main/webapp/resources/js src / main / webapp / resources / css和src / main / webapp / resources / js
Guys, any idea? 伙计们,有什么想法吗?
First when the application is deployed it checks the web.xml and gets 首先,在部署应用程序时,它会检查web.xml并获取
<url-pattern>/*</url-pattern>
Then it initializes the DispatcherServlet and initializes the initialization paramerters using 然后它初始化DispatcherServlet并使用初始化初始化参数
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
(WEB-INF must start with /) (WEB-INF必须以/开头)
then in mvc-dispatcher-servlet.xml
the view resolver resolves the rendering pages. 然后在mvc-dispatcher-servlet.xml
,视图解析器解析呈现页面。
Change <property name="prefix" value="WEB-INF/pages/"/>
to 将<property name="prefix" value="WEB-INF/pages/"/>
更改为
<property name="prefix" value="/WEB-INF/pages/"/>
Finally in your controller @RequestMapping("/getMessageStats")
最后在您的控制器中@RequestMapping("/getMessageStats")
so finally launching the application as 所以最后启动应用程序为
http://localhost:8080/ProjectName/getMessageStats/
invokes printWelcome() of GetMessagesStatsController and 调用GetMessagesStatsController的printWelcome()和
http://localhost:8080/ProjectName/getMessageStats/json
invokes generateJsonResponse() 调用generateJsonResponse()
Looks like the viewResolver has problem. 看起来viewResolver有问题。 change the prefix to /WEB-INF/pages/ , as follow: 将前缀更改为/ WEB-INF / pages / ,如下所示:
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
Without the slash it means a relative path, so it will find the page at relativePath(getMessageStats/) + prefix(WEB-INF/pages/) + request(getMessageStats) +suffix(.jsp). 如果没有斜杠,则表示相对路径,因此它将在relativePath(getMessageStats /)+前缀(WEB-INF / pages /)+ request(getMessageStats)+后缀(.jsp)中找到该页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.