• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with RequestMapping

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bill. I missed this dot (.) Thanks for reminding. Many thanks.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic