0

What I am trying to do is add on to the URL for my static HTML page to have request parameters. Subsequently, the page should consume them and call my Java api (in the same module) using the value(s) in the request parameter to dynamically build an HTML table. I have tried to add onto the URL like this via the ResourceHandlerRegistry:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("status.html")
                .addResourceLocations("classpath:/META-INF/resources/");
}

and altered the method to have request parameters:

registry.addResourceHandler("status.html?testparam=")
                    .addResourceLocations("classpath:/META-INF/resources/");

but get a 404 error when I try to do that.

The goal is to have the page be dynamic so that it gets created for someone based off the value of the request parameter they add. I know that the parameters can be parsed with javascript, it's just a matter of getting them to the page.

It seems to me that this wouldn't require a java code change, but I am not sure of the best way to go about doing this, if it's at all possible.

1 Answer 1

1

For this you don't need to define a separate resource handler. Just keep your status.html file in any of the default static resource locations

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/

You can modify/add/remove these default locations with spring.resources.static-locations property in your application.properties file. You can add a location. Even a location outside your jar from file system like this

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/path/to/static/directory

and access your page normally as

http://host:port/status.html?testparam=value

Or you can add static locations using resource handlers that you have mentioned in your question.

public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("abc")
                .addResourceLocations("file:/path/to/static/directory");
}

Here all the static resources in this loation will be accessed with a prifix 'abc' to your url

http://host:port/abc/status.html?testparam=value

Now in your status.html you can access url parameters and use them the way you want. Refer a simple way to access url parameters in javascript - https://css-tricks.com/snippets/javascript/get-url-variables/

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.