0

Let's say my website name is: foo.com

When a user types foo.com, I want to show index.html.

When a user types foo.com/something, I want the server catches the request at the controller.

Here is what I did in the HomeController:

@Controller
public class HomeController {
    @RequestMapping(value={"/"}, method=RequestMethod.GET)
    public String getHome() {
        return "index.html";
    }
}

And, the CustomController should catch the request

@Controller
public class CustomController {
    @RequestMapping(value={"/{custom}"}, method=RequestMethod.GET)
    public String getCustom(@PathVariable String custom) {
        // Do something here..
    }
}

However, it throws an error: Circular view path [index.html]: would dispatch back to the current handler URL [/index.html] again. It's because the CustomController catches the GET request: foo.com/index.html after the HomeController returns the string: index.html.

I did some research like this:

public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets"); // My asset
        registry.addResourceHandler("index.html").addResourceLocations("file:/index.html");
    } // It's not working

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/" + FileNames.INDEX);
    } // This also not working
} 

And changing the annotation from @Controller to @RestController in the CustomController is not an option.

Also, I don't have JSP files in the project - they are plain *.html files.

My project setup

I am using Spring 1.3.3 release, so please help me out.

1 Answer 1

1

This solution works with ui-router (AngularJS library). Also, you have to change $resourceProvider setting:

// In your app module config
$resourceProvider.defaults.stripTrailingSlashes = false;

Then, in the Spring server codes, you can do this:

@RequestMapping(value = "/{custom:[^\\.]*}")
public String redirect(@PathVariable String custom) {
  // Do something here...
  return "forward:/";
}

Found the solution at this link: https://spring.io/blog/2015/05/13/modularizing-the-client-angular-js-and-spring-security-part-vii

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.