Skip to content

Thymeleaf templates preceded by a slash are found when running in an IDE but not when running from a jar #1744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wilkinsona opened this issue Oct 22, 2014 · 13 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@wilkinsona
Copy link
Member

See http://stackoverflow.com/questions/26493831/spring-boot-thymeleaf-not-resolving-fragments-after-packaging. I'm not sure that there's anything we can do but I'd like to investigate.

@wilkinsona
Copy link
Member Author

It seems to be a basic limitation of the JDK. This small class:

public class TestResourceLoading {
    public static void main(String[] args) throws IOException {
        System.out.println(Application.class.getClassLoader().getResourceAsStream("foo/bar"));
        System.out.println(Application.class.getClassLoader().getResourceAsStream("foo//bar"));
    }
}

Run in an IDE:

java.io.BufferedInputStream@74e22632
java.io.BufferedInputStream@2b39d891

Run with java -jar:

sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@61bbe9ba
null

@wilkinsona
Copy link
Member Author

We could possibly do something in LanchedURLClassLoader to turn more than one consecutive slash into a single slash but I'm not sure it's worth it.

@philwebb
Copy link
Member

philwebb commented Dec 4, 2014

Seems like this might be a common mistake, it's been raised again in #2057. Striping double slashes might be an option but we need to check that this won't break existing code.

@philwebb philwebb reopened this Dec 4, 2014
@philwebb philwebb added the type: enhancement A general enhancement label Dec 4, 2014
@transentia
Copy link

Following on from #2507...with more (poss. important?) details.

I originally didn't have preceding '/' characters on my view names. The problem came when I introduced spring security:

   @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable();
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated();
        http
                .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/home")
                .failureUrl("/login?error")
                .permitAll()
                .and()
                .logout()
                .invalidateHttpSession(true)
                .logoutSuccessUrl("/login?logout")
                .permitAll();
    }

I found that I could not use simply 'login' instead of '/login', etc. (maybe SS is dong something different to Thymeleaf?) So I needed to find a solution that preserved the slash at the beginning of view names, while stripping the duplicate away. Thus I adopted the classpath configuration:

    spring:
      thymeleaf:
        cache: false
        prefix: classpath:/templates

@philwebb
Copy link
Member

philwebb commented Dec 5, 2014

Having review the behavior of the standard URLClassLoader I don't think double slash detection is something that we should do.

Given the following code:

InputStream url = Thing.class.getClassLoader().getResourceAsStream("sample//test.txt");
System.out.println(url != null);

Running the application from an IDE will print return true. Package into a jar and running using java -cp ./target/test-0.0.1-SNAPSHOT.jar sample.Test will return false.

This shows that our fat jar support is working in the same way as the standard JDK and I think it's best to keep consistency rather than introduce a difference and potentially break things in other ways.

@philwebb philwebb removed this from the 1.2.0 milestone Dec 5, 2014
@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed type: enhancement A general enhancement labels Dec 5, 2014
@izeye
Copy link
Contributor

izeye commented Jun 10, 2015

If it's a common mistake, I think giving a warning message would be helpful.

@opensource21
Copy link

Just as an extra information. I have a similar issue and there it works in the IDE and Tomcat. So I think the default jdk is inconsistent, would be great to get a warning.

@Dluquem
Copy link

Dluquem commented Jun 2, 2017

Yo tenia el mismo problema, la solución la encontré en este link. https://stackoverflow.com/questions/27249078/spring-boot-gives-templateinputexception-error-resolving-template-when-runnin

Okay, I found where you have to take a close look. In your Controller classes, when you use @RequestMapping on class, no leading slash (e.g. @RequestMapping("property")). On Method you need a leading slash (e.g. @GetMapping("/list")) and the return value no leading slash (e.g. return "property/list";)

An Example Snippet

@Controller
@RequestMapping("property")
public class PropertyController {
    @NonNull PropertyService service;

    @GetMapping("/list")
    public String list() {
        return "property/list";
    }
}

@evertones
Copy link

Is this issue resolved?
I've tried with version 1.5.10 and seems to be still happening.

I have the following code:

@Controller
public TestController {
    @RequestMapping(path= "/client/add", method = RequestMethod.GET)
    public String add() {
        return "/client/add";  // STARTS WITH A SLASH
    }
}

It works starting with Spring Boot using Maven: ./mvnw spring-boot:run.
It does not work with the jar: java -jar myapp.jar.

If I change the code above to return the template without the slash in the beginning, it works on both:

@Controller
public TestController {
    @RequestMapping(path= "/client/add", method = RequestMethod.GET)
    public String add() {
        return "client/add";  // DOES NOT START WITH A SLASH
    }
}

Is it the expected behaviour or this issue should've fixed that?

@AleixAlcover
Copy link

When you return the URI starting with a slash, you always send a double slash. The difference is that starting with Spring the file system deals with this issue, which doesn't do the JVM, when you run the .jar.

@ghost
Copy link

ghost commented Apr 13, 2018

I'm using Spring Boot 2.0.0.RELEASE and I'm experiencing this, I've tried removing slashes on my RequestMapping but I'm still getting HTTP 500. Is there a solution for this?

@AleixAlcover
Copy link

Have you checked the links in the .html (Thymeleaf template)? There you could be generating double slashes as well.

@snicoll
Copy link
Member

snicoll commented Apr 13, 2018

@oratengG4S we don't use the tracker for questions. Please ask on StackOverflow or join us on Gitter.

@spring-projects spring-projects locked and limited conversation to collaborators Apr 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

9 participants