File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Problem with RequestMapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem with RequestMapping" Watch "Problem with RequestMapping" New topic
Author

Problem with RequestMapping

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I want to map a request in my controller. The thing is I can have anything in the request url prefixed with a constant say "java". I should write a request mapping which will be generic for all urls containing java

It should work for

context_root/java/test.do
context_root/java/j2ee/test.do
context_root/java/core/test.do

I wrote these mappingsNow if I have another url like this

context_root/java/core/threads/test.do

I should definitely have another mapping like thisNow as the number of folders increase I have to keep on add @RequestMappings. Can I have a generic mapping instead of all these like a regular expression style. I tried the following but failedI could not figure out the logic. Is this possible actually?

Thank you all in advance. Good day.


Love all, trust a few, do wrong to none.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

is your resource on the file system like a file and the path is where on the file system it is located? What are you trying to accomplish?

FYI
I would get rid of the .do stuff too that is kind of old school now and you don't typically see it anymore in new applications.


[How To Ask Questions][Read before you PM me]
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Bill Gorder wrote:is your resource on the file system like a file and the path is where on the file system it is located? What are you trying to accomplish?

FYI
I would get rid of the .do stuff too that is kind of old school now and you don't typically see it anymore in new applications.

Actually I am having a controller which which redirects/forwards request to appropriate pages.

My folder structure is WEB-INF/jsp/static/java/<<sub-folder1>>/<<sub-folder2>>/ so on

Here <<sub-folder1>> has some pages in it. <<sub-folder2>> also has some pages in it. Again <<sub-folder2>> can have even more folders in it. Say <<sub-folder3>>. Or say let us stop at <<sub-folder3>> only. <<sub-folder3>> has some pages in it.

To redirect/forward to a page under these folders I have to write 4 @RequestMapping like thisMy question is can I group these four request mappings in to one? I tried using regular expressions but failed. I want only one @RequestMapping which should be called for

/root_context/java/view.do
/root_context/java/sub-folder1/view.do
/root_context/java/sub-folder1/sub-folder2/view.do
/root_context/java/sub-folder1/sub-folder2/sub-folder3/view.do

I want a request mapping such that it should be hit with any path starting with java/<<anything>>. I tried java/* It is only working for java/page.do, java/page2.do and so on. When I try java/sub-folder1/page1.do this request mapping is not being hit.

I hope this will explain well about what I want to accomplish.

Thank you, good day.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

Rather than putting all that information in the URL you could map it with a request param



Anyways if you are set on the other approach probably easiest to just leave it separate than trying the regex since you should have a finite number of directories with jsp's


chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you Bill, suppose that I have a finite number of directories say

java/sub-folder1/sub-folder2/sub-folder3

Can I write a regular expression for this? Is this possible?



Now how to escape this shash (/). I am not able to understand. Can I write like this? Is this kind of approach possible?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

Looking at the docs RequestMapping does not support regex. It does support ant style globs. You can use Regex however inside of your URI templates for example


URI template using regex



Request Mapping using ant style globs



This is actually demonstrated in the spring reference manual.

Now to your other question, even if it were possible I would lean towards one of the other approaches for a couple reasons:

1. Regex is harder to read and maintain
2. Regex is more error prone you may inadvertently match something you don't mean to
3. Regex does not add any real value imo, you can still use just one RequestMapping with either of the other approaches from the last post.


Hope this helps,

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

Sorry I missed the second part of your post



In this case path would equal the entire rest of the URL after java so it might be view.do or it might be sub-folder1/view.do

alternatively you can mark your path variable with required=false and just have optional ones.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you Bill. I missed this dot (.) Thanks for reminding. Many thanks.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi Bill I tried withIt is only working for

java/*.do

Its not working for

java/folder/*.do (or) java/folder1/folder2/*.do kind of urls

Then I tried thisIt is working fine for

java/x/*.do

When I try with

java/*.do

I am getting a NullPointerException because in this case @PathVariable String subFolder is failing. Is there any way I can say that this path variable is not always required?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

I searched few sites and came to know that many already tried what I am trying. I came to know that there is no property to define that a path variable is optional. The only way is to have two @RequestMappings or introduce request parameters like this.I hope this will help future readers. Good day.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with RequestMapping