0

I am developing a web app using Spring mvc. I have page which displays all projects in DB. If i click on any of the projects listed, it will display some other additional details of that particular project. This is done by using @PathVariable.

@RequestMapping(value={"/project/{name}"})
public String viewProject(HttpServletRequest request,@PathVariable("name")     
String projectName, ModelMap model){
     .......
     .......

 }

Above is my request mapping code. My url will be http://localhost:8083/releaseDashboard/project/CSOB.html (csob is my project name and releaseDashboard is my app name). Till this my app works fine. When i click on the home button from this page, my request is mapped to the above controller method and my url becomes localhost:8083/releaseDashboard/project/home.html. But the expected url is localhost:8083/releaseDashboard/home.html

Can anyone please help me? I read that we can use Interceptor or Filters to change the requested url. But i couldnt see any code snippet for that.

UPDATE

Web.xml

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

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

spring-servlet.xml

<context:component-scan base-package="com.suntec.reldashboard.controller" />
    <context:component-scan base-package="com.suntec.reldashboard.service" />
    <context:component-scan base-package="com.suntec.reldashboard.dao" />   
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    </bean>
sreehari
  • 189
  • 6
  • 16
  • Understood what do you want. But not clear what is your error and what you have done. Why the link has a `.html`? As it is Spring MVC, it must be `http://localhost:8083/releaseDashboard/project/CSOB`. – Ramanujan R Feb 20 '17 at 05:26
  • web.xml is configured like that. That is not an issue here. if i remove that html from web.xml, my url will be the same as you mentioned in your comments. – sreehari Feb 20 '17 at 05:31
  • Please edit your question with your config files - web.xml, spring config file etc. How you configured the view resolver? – Ramanujan R Feb 20 '17 at 05:32
  • @RamanujanR updated my question. Pls see – sreehari Feb 20 '17 at 05:37

1 Answers1

-1

Your config in web.xml in the context of Spring MVC is incorrect. Edit it as <url-pattern>/</url-pattern>. By this, all the requests to your project will pass through the 'dispatcher servlet'.

(You can also use like this <url-pattern>something-here</url-pattern>. Then your base url should have an extra 'something-here').

Now you can access the resource,

@RequestMapping(value={"/project/{name}"})
public String viewProject(HttpServletRequest request,@PathVariable("name")     
String projectName, ModelMap model){
     .......
     .......
    return "hello";
}

by the URL http://localhost:8083/releaseDashboard/project/CSOB. Then projectName will be CSOB.

You must have a 'jsp' file under /WEB-INF/jsp/ having the name hello.jsp. In that jsp file, you can access model values.

You must not use .html/.jsp within the URL, when using Spring MVC. All the resource is bounded to a VIEW, using view resolver. That is how this must be implemented. That's for it is 'MVC' and 'view-resolving'.

NOTE:

As per your current configuration, "you have to change a requested URL". NO, you can't. Then your URL may be http://localhost:8083/releaseDashboard/project/CSOB.html; and projectName is "CSOB.html". Then you have to use java substring function to extract "CSOB" from "CSOB.html". And this is an ugly stuff!

Ramanujan R
  • 1,601
  • 2
  • 23
  • 43
  • Thanks for your reply. So if i go to this url http://localhost:8083/releaseDashboard/project/CSOB it will show the contents of hello.jsp. Thats fine. But from this url, when i click on my home, it shows the url as http://localhost:8083/releaseDashboard/project/home and being mapped to the same controller method instead mapping it to a controller method with @RequestMapping(value={"home"}) – sreehari Feb 20 '17 at 06:06
  • "when i click on my home". Change url on that button. Obviously you are using a relative url. Make it right. Refer this. http://stackoverflow.com/questions/5559578/having-links-relative-to-root – Ramanujan R Feb 20 '17 at 06:10
  • Currently my action on home button is "home". Then what should be my url? – sreehari Feb 20 '17 at 06:14
  • `/home` i think. Or `/releaseDashboard/home`. Not sure. Check both and comment result – Ramanujan R Feb 20 '17 at 06:16
  • /home works fine. /releaseDashboard/home gives url as http://localhost:8083/releaseDashboard/releaseDashboard/home. Another thing that worked is action="../home" – sreehari Feb 20 '17 at 06:20
  • Dont use `../home`,as it depends on current page `/home` is good. – Ramanujan R Feb 20 '17 at 06:23
  • but what will happen if i have another url like this, localhost:8083/releaseDashboard/project/CSOB/52 , and i click on home from this page? From my understandings, if i give /home in action , my url will be localhost:8083/releaseDashboard/project/home. Correct? Then again my first issue will come. – sreehari Feb 20 '17 at 06:28
  • NO. `/home` makes the url root relative. So it will be `localhost:8083/releaseDashboard/home` from anywhere. More info : check SO link in above comment. – Ramanujan R Feb 20 '17 at 06:30