Question about applying static files to MVC view

31 views
Skip to first unread message

Tobias Erdle

unread,
Jan 30, 2019, 8:14:36 AM1/30/19
to MVC 1.0 Users Mailing List
Hi all,

I'm currently evaluating the MVC API and really like it so far. Unfortunately I stumbled over the problem, that any CSS I want to link within a view (in this case a really dumb JSP)
could not be found. I tried several approaches like moving the directory containing the CSS in the project, changing paths from project-relativ to context-relative etc in the JSP. but nothing
really worked. I also tried to use Webjars as it is done in this slightly outdated example (https://github.com/chkal/todo-mvc/blob/master/src/main/webapp/WEB-INF/views/items.jsp)
but even that didn't the trick. That leads to my question: What would be the correct way to solve this? I couldn't find anything in the MVC Spec regarding static assets,
so maybe there is some workaround for getting this to work (or it's a Layer 8 problem and I oversee something).

This is a small example project in which I tried to apply CSS to a MVC view: https://github.com/erdlet/ee4j-mvc-mailinglist-question
I'm using Wildfly 15 as application server (or maybe that is the problem?).

Thank in advance and best regards,

Tobias


P.S.: For a quick overview, here the JSP and my webapp directory:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EE4J MVC Example</title>
    <link href="${mvc.basePath}/webjars/materializecss/1.0.0/css/materialize.css" rel="stylesheet">
    <link href="${mvc.basePath}/assets/css/styles.css" rel="stylesheet">
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>



webapp_structure.png

Ivar Grimstad

unread,
Jan 30, 2019, 8:25:58 AM1/30/19
to MVC 1.0 Users
Hi Tobias,

Try to link to your CSS from the JSP pages as follows:

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/assets/css/styles.css">

Ivar

--
You received this message because you are subscribed to the Google Groups "MVC 1.0 Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsr371-users...@googlegroups.com.
To post to this group, send email to jsr371...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsr371-users/13759a0e-ba39-41dd-aa00-d7559bbbc64a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Erdle

unread,
Jan 30, 2019, 9:01:56 AM1/30/19
to MVC 1.0 Users Mailing List
Hi Ivar,

thanks for the hint. I just tried but got a 404 again :(

The network trace shows, that this URL should be fetched when using the request's context path:    http://localhost:8080/mvc-example/assets/css/styles.css For a comparison I tried again to use the MVC basePath and got the same target URL for the CSS: http://localhost:8080/mvc-example/assets/css/styles.css
The bold parts are the href's of the HTML links.

For me it looks correct, but is there maybe any additional configuration to do like it's in Spring MVC? When I remember correct, static asset access has to be configured there before any view can access it.

Best regards,

Tobias

Daniel Dias Dos Santos

unread,
Jan 30, 2019, 12:35:18 PM1/30/19
to jsr371...@googlegroups.com
Hello Tobias,

I tested here and now it's working.

1 - Add what Ivar mentioned above  = href="${pageContext.request.contextPath}/assets/css/styles.css"
2 - change the Path of classes:
@Path("/") public class ExampleController {

and

@ApplicationPath("/") public class MvcApplication extends Application { }

thanks.
--
Daniel Dias dos Santos
Java Developer
SouJava & JCP Member
Linkedin: www.linkedin.com/in/danieldiasjava


--
You received this message because you are subscribed to the Google Groups "MVC 1.0 Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsr371-users...@googlegroups.com.
To post to this group, send email to jsr371...@googlegroups.com.

Gregor Tudan

unread,
Jan 30, 2019, 1:53:06 PM1/30/19
to jsr371...@googlegroups.com
Hey Tobias,

It might be nothing, but I had problems with Wildly if the static resource path infers with the asset path.
So I usually set my @ApplicationPath to something different then the root context ( i.e. @ApplicationPath(„/hello“) ).

Give it try and see if that works. If it does, you can still auto-redirect the user to your application path with something like this in web.xml:

    <welcome-file-list>
        <welcome-file>/hello</welcome-file>
    </welcome-file-list>

- Gregor

 
--
You received this message because you are subscribed to the Google Groups "MVC 1.0 Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsr371-users...@googlegroups.com.
To post to this group, send email to jsr371...@googlegroups.com.

Christian Kaltepoth

unread,
Jan 31, 2019, 12:07:45 AM1/31/19
to MVC 1.0 Users
Hey all,

I think Gregor is correct. This problem usually occurs if you use @ApplicationPath("/"). You could try to use @ApplicationPath("/mvc") instead and then use ${pageContext.request.contextPath} for the assets like Ivar suggested.

Of course, it would be great to support the root application path for MVC apps, but this is actually a JAX-RS issue and depends on how the JAX-RS implementation intercepts the requests (servlet, filter, etc).

I hope this helps

Christian


For more options, visit https://groups.google.com/d/optout.


--

Tobias Erdle

unread,
Jan 31, 2019, 2:55:36 AM1/31/19
to MVC 1.0 Users Mailing List
Hi all,

thanks for your time & help. The @ApplicationPath change in combination with Ivars suggestion did the trick :)

Yeah the root application path support would be great for the future, but with the knowledge of this JAX-RS issue everything is
fine for me now. Only some kind of documentation for this behavior may be nice to help other users.

Thanks again & best regards,

Tobias

Christian Kaltepoth

unread,
Jan 31, 2019, 11:46:34 AM1/31/19
to MVC 1.0 Users
Hi Tobias,

great to hear it is working for you now. I agree that we need to document this somewhere. A bit more documentation would be great to have. But I guess finishing Krazo is top priority for now...

Christian

--
You received this message because you are subscribed to the Google Groups "MVC 1.0 Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsr371-users...@googlegroups.com.
To post to this group, send email to jsr371...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tobias Erdle

unread,
Jan 31, 2019, 12:21:08 PM1/31/19
to MVC 1.0 Users Mailing List
Hi Christian,

sure that makes completely sense. I'm really looking forward to a final MVC implementation :) 
In case I can support you in any way (even if it's only writing a few lines of documentation for the asset issue) please let me know.

Best regards,

Tobias

Christian Kaltepoth

unread,
Feb 2, 2019, 4:33:08 AM2/2/19
to MVC 1.0 Users
Hi Tobias,

thanks for your offer to help us. There are various ways to help us to move MVC and Krazo forward.

I tagged a few issues in our issue tracker with "help wanted". See here:


If there is anything you are interested in working on, feel free to tell us. Usually we can share some more details about these issues to help you to get started.

As you mentioned the documentation, this is also something which you could help with. You can find the full source code of the mvc-spec.org site here:


If you think something is missing in the documentation, feel free to open a pull request. Most pages are either written in Markdown or Asciidoc and adding more content should be really easy.

Christian






For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages