Reputation: 1567
I'm a beginner in springmvc and following a tutorial. But I got an error and it took me three days. So I would like to bring it here. First please look at my code:
web.xml
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Sitemesh: Decorates pages with layouts -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
servlet-context.xml
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:import resource="controllers.xml" />
My controller java class
@RequestMapping("/example")
public ModelAndView showMessage(
@RequestParam(value = "name", required = false, defaultValue = "World") String name)
throws IOException {
ModelAndView mv = new ModelAndView("example");
String response = readAll("http://localhost:53000/product");
JSONArray productsArray = new JSONArray(response);
products = new ArrayList<Product>();
if (null != productsArray){
for (int i=0; i< productsArray.length(); i++) {
JSONObject productobj = productsArray.getJSONObject(i);
Product product = new Product();
product.setId(productobj.getInt("id"));
product.setName(productobj.getString("name"));
product.setDescription(productobj.getString("description"));
product.setPrice(productobj.getDouble("price"));
product.setBalance(productobj.getInt("balance"));
product.setImageUrl(productobj.getString("image_url"));
specificationsMap.put(product.getId(), productobj.getJSONObject("specification"));
products.add(product);
}
mv.addObject("productList", products);
}
return mv;
}
I got an error:
No mapping found for HTTP request with URI [/springweb/example] in DispatcherServlet with name 'appServlet'
This is how I call the example
view:
<li><a href="<c:url value="/example" />">Example</a></li>
I can't find what wrong in here. Looking for help. Thanks.
Upvotes: 0
Views: 90
Reputation: 1567
I solved the problem by adding a <mvc:annotation-driven />
inside of controllers.xml class.
Thanks for your help.
Upvotes: 1
Reputation: 26
Write the servlet-context.xml code in file name appServlet.xml
or change code in web.xml
<!-- Processes application requests -->
<servlet>
<servlet-name>servlet-context</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet-context</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
lets hope this might work :)
Upvotes: 0
Reputation: 28519
You should change the mapping to spring mvc servlet from /
to /*
, so like
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The difference is that the meaning of the /
mapping is to catch all request not mapped by any other servlet, and since your sitemash filter maps all, that will never work. When you make the change you should also configure <mvc:resources>
element in order to exclude static files from mapping.
The mapping to servlet rules are defined by Servlet 3.0 specification
The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters. The URL path mapping rules below are used in order. The first successful match is used with no further matches attempted:
- The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
- The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.
- If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
- If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.
Upvotes: 0
Reputation: 858
c:url will automatically add the context root and jSessionid. This is the default behaviour of c:url and the purpose of using it.
If you don't need to add jsessionid, then you can go with simple <a href="/example">
If you have to use c:url, then you can try setting the context manullay, as <a href="<c:url context="/" value="/example" />">
Upvotes: 0