0

I'm new to Spring & Request Mapping and I currently have a maven web project setup that doesn't seem to handle my initial request properly

In my web.xml I have:

<servlet>
    <servlet-name>Example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/Example-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Example</servlet-name>
    <url-pattern>/</url-pattern> 
</servlet-mapping>

And my Example-servlet.xml:

<context:component-scan base-package="com.example.controller" /> //right package

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

And lastly my ExampleController:

@Controller
@RequestMapping("/")
public class ExampleController {

    @RequestMapping(value = "/start")
    public String start() {
        System.out.println("Example Starting...");
    }

}

My example directory structure:

enter image description here

however when I navigate to localhost:8080/example/ my page loses all of it's CSS styling and pretty much all calls to other files in the project. I get the Warning

No mapping found for HTTP request with URI [/example/css/main.css] in DispatcherServlet with name 'Example'

And a bunch of other warnings/404's for files I'm not trying to map.

enter image description here

Could someone help me understand exactly where my project is misconfigured?

Clay Banks
  • 4,483
  • 15
  • 71
  • 143
  • does [this](http://stackoverflow.com/questions/15070857/org-springframework-web-servlet-pagenotfound-no-mapping-found-for-http-request) help you – Ankur Singhal Sep 12 '14 at 06:00
  • can you please put in your view code please so we can verify if that is setup properly. Also your resource files setup (so where are you telling your spring confi where to look for resource files) – Aeseir Sep 12 '14 at 06:17

1 Answers1

0

Hi,

Your css and js files should be located in the resource folder that needs to be configured in servlet context configuration.

For example (Example-servlet.xml):

<resources mapping="/css/**" location="/css/" />
<resources mapping="/js/**" location="/js/" />

The means that all files located in {your webapp folder}/js/ will be automatically mapped to the path with prefix /js/ (and the same for css).

Using such configuration you will be able to load your js and css files in the following way:

<link rel="stylesheet" href="css/main.css">
<script src="js/util.js"></script>

Spring Configuration Example

sirdarpeace
  • 352
  • 3
  • 10