DaGLiMiOuX
DaGLiMiOuX

Reputation: 889

Spring - Mapping multiple url-pattern

I've asked something few days ago about this, but now came a new problem. My webpage need to access to these urls:

/product/* //(this will be an id or anything else)
/shopcart/* //(GET, ADD...)
/*.html

I have to access to different HTML pages for some webapp information. At the webapp, you can ask for some products by id and you can see a detailed webpage with all his specs. Also, I have a shopping cart where you can add the products and each time you add a product it will be saved at server's session.

If I want to recover information about products in cart, I access to /shopcart/get and get all information into JSON format. I have this config into my web.xml for that:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/product/*</url-pattern>
    <url-pattern>/shopcart/*</url-pattern>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

The problem comes that I can access to /product/1 to get all specs from product 1, but I get the same if I use /shopcart/1. If I specify @RequestMapping("product") (e.g.) into my controller I will have to access to /product/product/1 and I don't want that. How can I resolve this problem?

These are my controllers:

@Controller
public class ProductController {

    @RequestMapping("{id}")
    public ModelAndView get(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @PathVariable String id) {
        Map<String, Object> model = new HashMap<String, Object>();
        //data recovering
        return new ModelAndView("product", model);
    }
}

@Controller
public class CartController {

    @Autowired
    private JacksonConverter JacksonConverter;

    @RequestMapping(value = "/shopcart/get", method = RequestMethod.GET)
    public void get(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        // data recovering
        httpServletResponse.setContentType("application/json; charset=utf-8");
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        httpServletResponse.getWriter().print(json);
    }
}

Upvotes: 3

Views: 7701

Answers (1)

DaGLiMiOuX
DaGLiMiOuX

Reputation: 889

I got the soluiton for this problem.

First of all, thanks to RC for his answer in comments. The proper way that I wanted to use to access to my urls will be solved easily changing my servlet-mapping in my web.xml to this:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

And adding proper paths to my Controllers like this:

@Controller
@RequestMapping("product")
public class ProductController {

    @RequestMapping("{id}")
    public ModelAndView get(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @PathVariable String id) {
        ...
    }

@Controller
@RequestMapping("shopcart")
public class CartController {

    @Autowired
    private JacksonConverter JacksonConverter;

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public void get(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        ...
    }

But this isn't all you need. In my case, I couldn't load my CSS and JS resources. So, you need to specify to Spring where are your resorces, adding this into your servlet configuration (dispatcher-servlet.xml in my case):

<mvc:resources mapping="/resources/**" location="/resources/"/>

The /resources url must be changed on where your resources are. Now, you only have to add this into your JSP pages and all your resources will be loaded:

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/main.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/main.js"></script>

Link to resources solution: Spring can't load my css files

Tip: If you are moving your CSS and JS resources into a folder, like /resources/css and /resources/js, don't forget to build your project again, because your webpages will not access them.

Upvotes: 3

Related Questions