1
votes

I'm trying to start up Jetty with Spring MVC that contains jsp and @Controller. After start jetty I've got every time WARN if I try to call any pages. For example

ttp://localhost: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'

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

<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

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

    @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/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml; src/main/webapp/WEB-INF/pages/*.jsp; src/main/webapp/resources/css and src/main/webapp/resources/js

Guys, any idea?

2
try accessing like this localhost:8080/ProjectName/pageName.jspSparkOn

2 Answers

3
votes

First when the application is deployed it checks the web.xml and gets

<url-pattern>/*</url-pattern>

Then it initializes the DispatcherServlet and initializes the initialization paramerters using

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

(WEB-INF must start with /)

then in mvc-dispatcher-servlet.xml the view resolver resolves the rendering pages.

Change <property name="prefix" value="WEB-INF/pages/"/> to

<property name="prefix" value="/WEB-INF/pages/"/>

Finally in your controller @RequestMapping("/getMessageStats")

so finally launching the application as

http://localhost:8080/ProjectName/getMessageStats/

invokes printWelcome() of GetMessagesStatsController and

 http://localhost:8080/ProjectName/getMessageStats/json

invokes generateJsonResponse()

1
votes

Looks like the viewResolver has problem. change the prefix to /WEB-INF/pages/, as follow:

<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).