简体   繁体   中英

Some doubts about request mapping in Spring MVC

I am studying for Spring MVC and on the study material I have the following question on which I have some doubts:

Assuming a web application context name of "rewardsonline" , a servlet mapping of /admin/* , and an incoming URL of " "rewardsonline/admin/accounts/show" what is the URL used from Spring MVC request-mapping purpose?

  • /rewardsonline/admin/accounts/show

  • /admin/accounts/show

  • /accounts/show

  • /show

I think that the correct answer is /accounts/show and I have reason in the following way: rewardsonline is the applcation name (the application context name is the application name**.

Whereas /admin/accounts/ is the servlet mapping and finnaly show is the request mapping (mapped on a specific method).

Is it reasoning correct or am I missing something?

Your assumptions are correct

given this web.xml fragment

<servlet-mapping>
    <servlet-name>your org.springframework.web.servlet.DispatcherServlet</servlet-name>
    <url-pattern>/admin/*</url-pattern>
</servlet-mapping>

in your classes you must write

@RequestMapping(value = "/accounts/show", method = RequestMethod.GET)
...your method starts here

to map class methods to single url

remember to put this

<context:component-scan base-package="the package of the classes you want to map"/>

in your spring configuration

Correct. As you said "rewardsonline" is application context. /admin/* is servlet mapping which means any request coming from /admin/ will be handled by spring mvc. /accounts/show is used for request mapping purpose. Controller with mapping /accounts/show will handle this request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM