0

I'm making my first steps in Spring MVC, and I'm probably missing something, since this doesn't make sense to me:

I started with the mvn-basic spring sample, and got it to run. Then I wanted to see if I can make it work on a sub-path, so that I can use it alongside legacy code that I have. I made the following changes:

In servlet-context.xml:

-   <mvc:view-controller path="/" view-name="welcome"/>
+   <mvc:view-controller path="/web/" view-name="welcome"/>

In AccountController.java:

-@RequestMapping(value="/account")
+@RequestMapping(value="/web/account")

In web.xml:

-       <url-pattern>/</url-pattern>
+       <url-pattern>/web/*</url-pattern>

I also increased the logging to DEBUG. I rebuilt and tried running, but trying to access http://localhost:8080/web/account resulted in 404, and "No mapping found" in the log, even though earlier in the log I can see "Mapped URL path [/web/account] onto handler 'accountController'".

I discovered that if I undo the changes to web.xml, everything works, but then the DispatcherServlet takes over all the requests.

So I have two questions:

  1. What am I doing wrong? How can I limit the DispatcherServlet to a subsection of the site?
  2. How does it even work in the first place? The demo seems to only send requests to "/" to the DispatcherServlet, so how come "/account" also works?

I'm using Spring 3.0.5.RELEASE

UPDATE: The solution is to leave the request mapping is at was, thanks @axtavt. Otherwise the URL becomes http://localhost:8080/web/web/account (notice the duplicate /web). I would still like an answer to my second question, though.

itsadok
  • 28,822
  • 30
  • 126
  • 171

2 Answers2

2

You don't actually need to do any modification

In servlet-context.xml: and web.xml:

if you want your path to be "/web/account" then your request mapping will be

@RequestMapping(value="/web/account")

and you need call it with "account" if you are already in web (for example http://localhost/urproject/web/currentpage) directory when you call the view if your directory is not in web (for example http://localhost/urproject/currentpage) then call it with "/web/account"

I hope that helps

Pinchy
  • 1,506
  • 8
  • 28
  • 49
1

With this setting:

 <url-pattern>/web/*</url-pattern>

and

 <mvc:view-controller path="/web/" view-name="welcome"/>

and

  @RequestMapping(value="/web/account")

The URLs accessible are:

http://localhost:8080/web/web/ --> as defined as view-controller config http://localhost:8080/web/web/account --> as defined in the controller request mapping

If you want to access account page as /web/account only, redefine your request mapping to:

@RequestMapping(value="/account")
gouki
  • 4,382
  • 3
  • 20
  • 20