- This topic has 6 replies, 2 voices, and was last updated 14 years, 4 months ago by davemeurer.
-
AuthorPosts
-
RobertGloverJrMemberI am trying with no luck, to use the Spring Annotation Builder to modify the method shown below so that I can display to System.out the value of request.getHeader(“user-agent”).
@RequestMapping("/indexEmployee") public ModelAndView listEmployees() { ModelAndView mav = new ModelAndView(); mav.addObject("employees", employeeService.loadEmployees()); mav.setViewName("employee/listEmployees.jsp"); return mav; }
I watched the YouTube video “Spring Tutorial – Code Assistance for Spring framework stereotypes”, but it did not cover my category of question.
I properly enabled “Spring DSL” for controllers only. However when I go to the Spring tab and select method “listEmployees”, the annotation options do not seem to apply to my need here.
How can I use the annotation builder to specify that I want to be able to access the value of request.getHeader(“user-agent”) ?
RobertGloverJrMember@RobertGloverJr wrote:
…snip…
How can I use the annotation builder to specify that I want to be able to access the value of request.getHeader(“user-agent”) ?As a point of clarification, I am not suggesting that the ME4S annotation helper is in any way not doing what it is advertised as being able to do. I am probably asking for a feature enhancement and, meanwhile, a manual, “by hand” annotation that I can add to that function so that I can directly access the request object so that I can ask the request object what the agent type is.
RobertGloverJrMemberI just noticed the following method in the EmployeeController that was generated by ME4S:
@RequestMapping("/employeeController/binary.action") public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) { ModelAndView mav = new ModelAndView(); mav.setViewName("streamedBinaryContentView"); return mav; }
Then I clicked on the method name and then clicked on the Spring tab. I accidentally clicked the “@ModelAttribute” label within the Spring tab, and a help page opened up in a browser that explained how to use the @ModelAttribute annotation !!!
Wow. That really should be put into a YouTube video. Who would have guessed? And THEN, there was within the ME4S online help for @ModelAttribute a link that said, “See also: @ModelAttribute [Spring Framework – Reference Documentation]”. And when I clicked on IT, it brought up the section about @ModelAttribute in the Spring reference documentation !!!
In the Spring reference documentation is explained the following:
13.11.4. Supported handler method arguments and return types
Handler methods which are annotated with @RequestMapping are allowed to have very flexible signatures. They may have arguments of the following types, in arbitrary order (except for validation results, which need to follow right after the corresponding command object, if desired):
Request and/or response objects (Servlet API). You may choose any specific request/response type, e.g. ServletRequest / HttpServletRequest.I am really impressed. Now I have the answer to my question.
RobertGloverJrMemberI guess I’m not quite there yet….
When I use this method:@RequestMapping("/indexEmployee") public ModelAndView listEmployees(@ModelAttribute HttpServletRequest request) { System.out.println("user-agent= "+request.getHeader("user-agent")); ModelAndView mav = new ModelAndView(); mav.addObject("employees", employeeService.loadEmployees()); mav.setViewName("employee/listEmployees.jsp"); return mav; }
I get this error:
Aug 24, 2010 8:22:36 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet testiphone1 Servlet threw exception org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [javax.servlet.http.HttpServletRequest]: Specified class is an interface at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:659) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:293) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) at org.springframework.web.servlet.D
RobertGloverJrMemberOkay, it’s fixed now. The correct method is:
@RequestMapping("/indexEmployee") public ModelAndView listEmployees( HttpServletRequest request) { System.out.println("user-agent= '"+request.getHeader("user-agent")+"'"); ModelAndView mav = new ModelAndView(); mav.addObject("employees", employeeService.loadEmployees()); mav.setViewName("employee/listEmployees.jsp"); return mav; }
So for example, in Safari id displays:
user-agent= 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-us) AppleWebKit/533.17.8 (KHTML, like Gecko) Safari/412.0'
But when I use Safari developer tools to set agent header to iPad it displays:
user-agent= 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16'
and when I set the agent type to iPad it displays:
user-agent= 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10'
and when I set the user agent in Safari dev tools to iPod it displays:
user-agent= 'Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16'
RobertGloverJrMemberAnd so finally, I can make an intelligent suggestion for improving the “Spring” tab that ME4S provides to help with annotations.
It would be very helpful if it provided a drop down list that showed the various arguments that are permitted to be added when the method is annotated as “@RequestMapping.
For example, according to the Spring documentation, these arguments are some of those that would be permitted:
- ServletRequest
HttpServletResponse.
HttpSession
Locale
etc.It’s rather confusing which arguments are allowed, so I think this would be a big help with the already very useful ME4S annotation helper.
davemeurerMemberHi Robert,
Thanks for the forum post! I’ve added an improvement request to the Spring Annotator per your request.
Kind regards,
Dave -
AuthorPosts