3

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);
    }
}
2
  • map the dispatcher to / and use annotations to map each controller/methods to their url(s) Commented Feb 21, 2014 at 18:37
  • I'm dissapointed :/ I was trying to find a way to solve my problem with my configuration when the most easy way was there all the time, but now I can't access to my CSS and JS resources :S and they are not into my WEB-INF Commented Feb 21, 2014 at 18:48

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.