EdgarZeng
EdgarZeng

Reputation: 3708

How to deal with the url with value like root=, the parameter with key and without value?

I have the problem just like the title. I know there is a @RequesetParam in spring mvc, it has a parameter required=false. But it failed when try to use it.

Actually I found the @RequestParam is useless. According to my trying, It always be O.K. using @RequestParam(value="root", required=true) where the URL is with/without value=some value except blank. So, I don't know what totally the required=true is. Then, whichever the required=false or required=trueis, it throws exception with URL parameter like ...&root=.

And my HandlerMapping & AdapterMapping are RequestMappingHandlerMapping and RequestMappingHandlerAdapter

the version of spring mvc is 3.2.4

Please tell me what's going on with the spring mvc?

Thanks advance:)

Upvotes: 0

Views: 266

Answers (2)

M2hp
M2hp

Reputation: 205

According to the documentation:

public abstract boolean required

Whether the parameter is required. Default is true, leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.

Alternatively, provide a defaultValue, which implicitly sets this flag to false.

so if the parameter is missing inside a request, you can decide whether an exception should have been thrown or not !

according to your question you are amazed why no exception does throw, It's because these two URL have different meaning :

http://my-domain.ir:8080/your-project-name/main.html?p=something&root=

http://my-domain.ir:8080/your-project-name/main.html?p=something

in the first case the value root exist but It doesn't have any value But in the second case it is missing, Your case meet the first condition and this is because no exception does throw .

//if you use the code below, in your case the root will get no value exactly like you define String root =""
@RequestParam("root") String root

I suggest you always use @ModelAttribute in spring and validate/not validate your Bean class variables as you wish with JSR303 standard and receive your Parameter value using the GETTER/SETTER methods of that bean class.

Upvotes: 2

Abhishek Nayak
Abhishek Nayak

Reputation: 3748

You should use placeholder to get value in URL and use @PathVariable to get the values from url,

for example:

@RequestMapping(value="/customer/{customerId}/show", method = RequestMethod.GET)
public ModelAndView showCustomerById(
            @PathVariable("customerId") int customerId) {
  ...
}

and the requested url will look like:

http://localhost:8080/HelloWorld/customer/1234/show

and if you want to pass parameter with above then, do like:

 @RequestMapping(value="/customer/{customerId}/show", method = RequestMethod.GET)
public ModelAndView showCustomerByIdAndName(
            @PathVariable("customerId") int customerId,
            @RequestParam(value = "name", required = false) String name){
 ...
} 

and the requested url will look like now:

   http://localhost:8080/HelloWorld/customer/1234/show?name=myName

Note: the default value of required field is true, and if the parameter is not found then exception will raise.

Upvotes: 1

Related Questions