Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

DispatcherServlet: redirect on page at localhost

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'am trying to test my @RestController on abbility of redirecting.

For testing aims it must redirect on localhost.



Servlet mapping is:



I run application and enter, for example:



But I receive an 404-error.

When I use instead of "access_approved" absolute-path of real-sites (for example, "https://stackoverflow.com/") its working well.

So, the problem is focused on pages, which located at my-project-structure.

In my webapp-directory I have 2 html-files:

access_denied.html
access_approved.html.


 
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the absolute URI that you are redirected to, and where under the webapp folder are your views located?
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What is the absolute URI that you are redirected to, and where under the webapp folder are your views located?



  absolute URL is   localhost:8080/test/

  webbapp-folder in a root of project, named "test"

  test/webapp/access_approved.html

  test/webapp/WEB-INF/web.xml
 test/webapp/WEB-INF/access_denied.xml
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there's your problem.

Resources inside the WEB-INF folder are private. A client can not access them by browsing to them directly. Instead of redirecting to those pages, you must forward to them. You do this by returning a view from your method. However, since you're using a @RestController, your methods won't return views; they will return response bodies instead.

You need to make a decision. Do you want your controller to be RESTful or not? If yes, then don't forward to an HTML page. Just return JSON or XML data for the client to process. If no, then replace the @RestController annotation with @Controller, and return the route of the view you want to display.
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst, yes, i understand, that it was bad idea to test @RestController on redirecting on localpages inside webapp.

But now I'am trying to run my simple index.html page during running tomcat-app. Without any methods-invoking.
test/webapp/index.html.

But I receive the same error: 404.

 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the URL you're browsing to, your web.xml and your context.xml. If you're using annotation based component discovery, show us your main application config class.
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://localhost:8080/test/

web-xml:



context.xml:







 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the name of the built WAR file?

Please show us the contents of the XML files in the spring folder.
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   text:war
   test:war eplored

Application context:  /test

spring-app



spring-db:



spring-mvc:






 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean, what is the literal file name of the built WAR, including the .war extension?
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I mean, what is the literal file name of the built WAR, including the .war extension?



test.war
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your DispatcherServlet is listening for all requests under localhost:8080/test. You likely don't have an MVC controller for the root path, so Spring returns a 404.

You either need to add a simple controller that forwards requests to your static pages, or you need to register a separate servlet that handles your static resources.
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
    I add to spring-mvc.xml the following:

 

   and controller:



directory: test/webapp/access_approved.html.


It redirects me on http://localhost:8080/test/access_approved.    But with the same 404-error.

I tried "forward:access_approved"
But it forwards me on http://localhost:8080/test/
 
Andrey Dmitriev
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
    I have found the solution:

   I add separate controller:

 

And conroller for the root-mappring:



new spring-config:



Besides I remain @RestController.  

So this solution cost for my project architecture-sophistication...
 
bacon. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!