1

I am working on a project using hibernate and Spring MVC architecture. My problem is that I am using (*.htm) url pattern in my web application , in this case when I sent a product's id to controller for editing, the product's id is shown in url for eg.

"localhost:8080/MyApp/editProduct.htm?productId=03".

But I don't want this . I just want

"localhost:8080/MyApp/editProduct.htm?productId" or "localhost:8080/MyApp/editProduct.htm/productId/03" 

and I am unable to use @PathVariable Annotation in my controller because of my url pattern(*.htm) and using of @PathVariable Annotation the JSP page never load properly. Any Suggestions . Thanks in Advance.

Controller:-

@RequestMapping(value = "/{sId}/deleteState.htm")
public ModelAndView deleteState(@PathVariable("sId") int sId ){
    ModelAndView mav = new ModelAndView();
    
    try{
        stateDAO.deleteById(sId);
        mav.addAllObjects(prepapareModel());
        mav.addObject("msg", "State Deleted Succesdfully");
        mav.setViewName("admin/viewState");
        return mav;
    }catch(Exception e){
        e.printStackTrace();
        mav.addAllObjects(prepapareModel());
        mav.addObject("err", "Failed to Delete State");
        mav.setViewName("admin/viewState");
        return mav;
    }
}

public Map prepapareModel(){
    Map map = new HashMap();
    map.put("states", stateDAO.findAll());
    return map;
}

Url after deleting the State from database:-

http://localhost:8080/PujaInBox/10/deleteState.htm

I think the Id of State is creating the problem . 10 is Id of state.

flanker5
  • 88
  • 1
  • 7
msk
  • 110
  • 1
  • 1
  • 11
  • either use hidden parameter to pass the value. or encode URL. – Prashant Mar 09 '15 at 07:39
  • If you're planning to use path variables then URL should be `localhost:8080/MyApp/productId/03/editProduct.htm` ending with your extension – Arkantos Mar 09 '15 at 07:42
  • Also If you actually want to hide your parameters, why don't you fire a POST request instead of a GET request ? Even with path variables, it's easy to find the `productId` values you're sending in URL and they are available in browser history as URLs, so it's not that safe :) – Arkantos Mar 09 '15 at 07:44
  • @Prashant , I am not using form , so how can i use hidden parameter. Is it possible ??? – msk Mar 09 '15 at 07:44
  • So is it just a Hyper link ? – Arkantos Mar 09 '15 at 07:44
  • @Arkantos yes. Exactly . – msk Mar 09 '15 at 07:45
  • Then your can try the URL in mentioned in previous comments. Also it's better not to use localhost because you will have problems when you deploy this in an actual server. So change your `href` of hyper link like this `/MyApp/productId/03/editProduct.htm`.. That should work :) – Arkantos Mar 09 '15 at 07:50
  • @Arkantos thanks bro. I am gonna try your solution. – msk Mar 09 '15 at 07:53
  • I've posted my answer with corresponding Spring controller changes.. see if that helps :) – Arkantos Mar 09 '15 at 07:57

4 Answers4

0

Change servlet default url pattern to:

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

But in this situation better option is to manage your @PathVariables in the middle part of URL, like:

/myapp/product/{productId}/edit.html
kxyz
  • 802
  • 1
  • 9
  • 32
  • Come on.. if he changes the servlet mapping then that's invoked even for static files – Arkantos Mar 09 '15 at 07:48
  • @kxyz i tried this already , but its not working for me. Because I am using Security Filter with url pattern (*.htm). And when i change the url pattern of web.xml it throws Exception. – msk Mar 09 '15 at 07:52
  • Just use second solution. Because first solution is more flexible and requires some changes in your project. Until is simple application it's not needed. – kxyz Mar 09 '15 at 07:52
0

If you want to make use of @PathVariable annoation, then URL should be

/MyApp/productId/03/editProduct.htm - ending with your extension and your controller mapping should be like this

@RequestMapping(value="/productId/{id}/editProduct")
public String editProduct(@PathVariable String id, Model model) {

}

One more change to note here is that I mentioned a relative URL instead of absolute URL like localhost:8080/MyApp/productId/03/editProduct.htm including host name and port. This won't work when you deploy your application in an actual server because localhost always refers to your current machine but your application is deployed on some other host.

Hope that makes sense :)

Arkantos
  • 6,530
  • 2
  • 16
  • 36
  • Its working , but my JSP page is not loading properly, means CSS of page is not working when i am using @PathVariable Annotation. Why is this happening. ? – msk Mar 09 '15 at 08:07
  • Did you see if your css files are loaded in Dev Tools -> Network tab ? Also can you post the exact URL changes you did to your hyper link ? – Arkantos Mar 09 '15 at 08:09
  • Before :- delete now :- delete – msk Mar 09 '15 at 08:13
  • Did you check if the URL in browser address bar is same as what you're expecting after clicking the hyper link? – Arkantos Mar 09 '15 at 08:16
  • yepp , everything is working fine . I mean I wanna delete State , its done . But when i want to redirect the list page of state , nothing display css related content like images fonts etc on jsp. But everything is in simple text format. – msk Mar 09 '15 at 08:21
  • I am unable to understand that is there any complications between CSS and @PathVariable Annotation. – msk Mar 09 '15 at 08:23
  • Did you check in your redirected page, all CSS and images are included ? Check if you're missing some common header.jsp or something – Arkantos Mar 09 '15 at 08:23
  • Maybe you have restricted the CSS Resources by your Security Filter? – sven.kwiotek Mar 09 '15 at 08:30
  • @Arkantos Yess , Everything is set fine. Because I am not changing anything in state List page , inter change in controller between '@RequestParam' to '@PathVariable' its all happening . – msk Mar 09 '15 at 08:34
  • Hmm.. that shouldn't be a problem as far as I know.. did you check the page you're getting in DevTools - Network tab ? Also can you add your Controller mapping with path variable declaration, your URL in browser after clicking on hyper link ? Pls add them to your question not in comments :) – Arkantos Mar 09 '15 at 08:38
  • add your controller method code and jsp where you reffer to controller, also in jsp you should use c:url or spring:url tags for links, http://stackoverflow.com/questions/5007210/how-to-use-springurl-with-an-a-tag , also I think you will be intrested in ajax technologies – kxyz Mar 09 '15 at 08:45
  • @kxyz thanks , I am going to try your suggestion about 'spring:url' – msk Mar 09 '15 at 09:13
  • So you're absolutely sure that your controller method is invoked, your DB entry is deleted and redirected to `viewState.jsp` ?? – Arkantos Mar 09 '15 at 09:15
  • @Arkantos , yess . State deleting properly , and updated list of state showing on 'viewState.jsp' . just css is missing . – msk Mar 09 '15 at 09:22
  • @kxyz I think it will not work. Because front Controller will not recognize the request , its accepts only *.htm url pattern. – msk Mar 09 '15 at 09:25
  • @Sohail.. it will work because you already mentioned `.htm` in your web.xml for DispatcherServlet, so with or without extension it will work – Arkantos Mar 09 '15 at 09:30
  • @kxyz.. removing the extension will not have any effect here. It will be just more specific match :) – Arkantos Mar 09 '15 at 09:31
  • Ok Right click on your page - View Source - Search your css file. Check if it's enabled like a hyperlink, if so click on it and see if you're able to access that file – Arkantos Mar 09 '15 at 09:33
  • @Arkantos yess i just checked right now. All css are working . – msk Mar 09 '15 at 09:38
  • So your page is rendering properly ? – Arkantos Mar 09 '15 at 09:39
  • Sorry I didn't get you. rendering means what. – msk Mar 09 '15 at 09:43
  • Dynamic Data is displaying, only design is not displaying. And i also tried without .htm , still same problem. – msk Mar 09 '15 at 09:44
  • I mean to ask if your page is displayed properly. If code is working, CSS files are downloaded properly then probably some issue with your HTML, check if all elements have proper IDs, classes and other names – Arkantos Mar 09 '15 at 09:47
  • If you still think `@PathVariable` is the problem, you can create a dummy request mapping without path variable and in that method, always redirect to `admin/viewState` without deleting anything, just retrieve states and redirect to that JSP and see if it's working fine – Arkantos Mar 09 '15 at 09:56
  • Without `@PathVariable` its working absolutely fine . – msk Mar 09 '15 at 10:03
  • Then we need to debug it more closely, unfortunately you don't have enough rep to be a part of some chat – Arkantos Mar 09 '15 at 10:11
  • Yepp I will debug , but i don't think so that its gonna be resolve. I will try something else otherwise. – msk Mar 09 '15 at 10:18
0

I am doing an assignment in a class.

I am guessing the first step is to use wildcard (*) to map your URL. In your pages, you can use whatever the URL you prefer.

Then you can invoke the method getPathInfo() in your servlet.

String action = request.getPathInfo();

Or you can call getHeader("referer") to get your URL then manipulate your string to get the information you need.

Finally, you can put the string in your if-else or switch statement.

Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
0

All you have to do is create a form in your html page with method=post

<form method="post">

Then create fields inside this form and if you don't have any fields that should be displayed used input with type hidden.

<input type="hidden" name="parametername" id="parameterid" value="parametervalue">

This will do the trick, it won't show any parameter value in the URL and you can access the values as you already do.

Hope this helps....

muhammed aslam
  • 44
  • 1
  • 11