Intellij Not Recognizing Model Variables in HTML. How to resolve model variables. I don't get any idea for this issue.
Here is my Controller
#Controller
public void someController {
#RequestMapping("/")
public String someMethod() {
model.addAttribute("message", "message");
return "index";
}
And here is my "index.html"
<p th:text="${message}"> </p>
and of course in my html tag i'm using thymeleaf :
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
the problem is in myth:text="${message}" i see red squiggly lines saying that "Cannot resolve "message" variable..."
For recent versions of IntelliJ:
With the cursor on the variable, press Alt-Enter and you should see a menu option to "Declare external variable in comment annotation". When you select this option, you'll get a comment template with the cursor positioned to type in the data type of the variable.
When complete, you'll have something that looks like this:
<!--/*#thymesVar id="productIds" type="java.util.Map"*/-->
<div data-th-each="p : ${productIds}">
The Alt-enter menu doesn't seem to work within expressions such as ${#maps.isEmpty(productIds)}. In this case, manually creating the comment might make the UI get rid of the "unresolved" indicator.
I've been ignoring that issue for as long as I've been using Thymeleaf. Even though it shows the squiggly lines, it should still work when you run the application.
IntelliJ would almost have to compile the code in the background to be able to automatically (and accurately, since you could have multiple methods who uses the same template) resolve the variables.
I've never given a tip like this, but after reading your comment that you just find the wiggly line annoying, I decided to suggest it anyways:
Disable the tip.
I feel absolutely barbaric for posting this answer, forgive me SO
these red lined could be very annoying I just removed www. from my thymleaf and it worked
In your .html thymeleaf file Replace line 1 with line 2
Line1 : <html xmlns:th="http://www.thymeleaf.org">
Line2 : <html xmlns:th="http://thymeleaf.org">
Just stumbled upon this while having the same problem.
In my case it was caused by having the #SpringBootApplication class not in the same Maven module as the template and controller. Including the application module in the pom.xml with <scope>provided</scope> solved it for me.
I think #TwiN is right. IntelliJ indeed has to compile the whole thing and since it couldn't find an application it had no idea how to make a component lookup.
Related
the angularjs feature called directive cause to problems in spring mvc. If I use thymeleaf to render a html with elements such
<div ui-view autoscroll="false"></div>
i got a error like
org.xml.sax.SAXParseException: Attribute name "ui-view" associated with an element type "div" must be followed by the ' = ' character.
is there an elegant workaround or should I use something else than thymeleaf?
Edit:
Many thanks for your answers, they helped me a lot.
Either you code xml or you use some workaround. open your application.properties and add following
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
Just put the below code. basically what it is saying is that every attribute in HTML should have a value. When the browser renders it, it will anyway look like below.
<div ui-view="" autoscroll="false"></div>
Update: You can also use directive in a class or as an element.
I am just getting started working with the play framework, and I'm trying to understand the interaction between java application code, and the scala-based template framework (Note: I know absolutely nothing about Scala so far, beyond the fact that it's another language that compiles to bytecode on the JVM, and that your Scala and Java classes can interact).
I have a test1.scala.html template that looks like this:
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
</head>
<body>
#content
</body> </html>
As you can see from the top line, the template expects a String and an Html argument, but I can't figure out how to construct the Html argument from Java caller code!
I have tried a few variations in my controller class:
return ok(test1.render("My Title","It <em>finally</em> works!"));
This fails, obviously, because the second argument is a String and not Html, so I have an argument mismatch. (There's a runtime error: actual argument String cannot be converted to Html by method invocation conversion -- which makes sense, but I was hoping for some magic here. :))
So I tried creating some Html from a String, figuring this was a likely helper class somewhere in the package and this might 'just work':
return ok(test1.render("My Title",new Html("It <em>finally</em> works!")));
This won't compile, because javac can't find an Html class. Ok, fair enough. Scanning the play documentation, there appears to be a play.api.templates.Html class (written in Scala) with a constructor that takes a String, so I try the full package-qualified name:
return ok(test1.render("My Title",new play.api.templates.Html("It <em>finally</em> works!")));
And this won't compile either: I get a Symbol not found for 'Html' in package play.api.templates.
So: what's the magic sauce that will let me turn my String (which contains a snippet of HTML) into an HTML object I can pass into the template?
Play templates have been factored out into the Twirl module, as stated in the Play 2.3 Migration Guide.
play.api.templates.Html is now play.twirl.api.Html.
I am following the same tutorial as this guy, so my code is identical to his in this post: Play 2.0.4 file upload. NullPointerException: null
however, my problem is different in that i have a compilation error, not a runtime error(yet). I type "start" in the play console, and the compiler tells me:
not found: value form
This is the content of my index.scala.html:
#form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="picture">
<p>
<input type="submit">
</p>
}
Play is complaining it doesnt recognize the form template tag "#form".
According to this article, http://dylankobayashi.wordpress.com/2013/05/09/views-and-play/ the author thinks it's a windows specific issue, but I don't know what he means by CR and LF.
Finally, I was reading the play 2.1.3 javadoc for the MultiPartFormData class and didn't really understand it; can this accept any kind of file, including zip files? Thank you in advance
You're probably just missing the import. The form tag (and all other built-in form element tags) are in the views.html.helper package.
You need to either include the package name when you use the tag:
#helper.form(...)
Or add the import at the top of your view:
#import helper._
The ._ at the end is the Scala equivalent of .* in Java imports.
I am using sitemesh for a spring based site. The problem is that I have some javascript that I want it to run on the onload event of only one specific page using jquery $(function() { ... }) and not on every page.
I have included the jquery.js in the bottom of my decorator, after the body. So, if I try to include a <script> tag in the body of my decorated page the script won't be executed because jquery will be loaded after that! I know that I could include the jquery.js in the header of my decorator so it will be before the custom script in the decorated page however I don't really like that solution since it will contain javascritp in the head of my page.
So I would like to have something like a placeholder in my sitemesh decorator in where the custom from my decorated page will be placed (as you can understand I come from the django world :p). Is this possible ? Do you propose anything else or should I just put my jquery.js in the header and be done with it ?
To answer my question, after some search I found the following solution:
I Added the following to the end of my decorator page (after including jquery.js)
<decorator:getProperty property="page.local_script"></decorator:getProperty>
I also added the following
<content tag="local_script">
<script>
$(function() {
alert("Starting");
});
</script>
</content>
The decorated result page contained the contents of the local_script tag exactly where I wanted them and everything worked fine :)
I don't know why this feature of sitemesh is not properly documented - using this you can have a great templating behaviour (like django).
I'm having a problem with my Errai GWT app which I get this error when the page tries to TransitionTo a page, say FormsPage:
No page with a widget type of com.mycompany.myproject.client.local.FormsPage exists
Even if the FormsPage is there in the package and there is no-compile time error (so it was able to locate the page) in:
TransitionTo<FormsPage> formsPage;
What could be the problem?
I forgot the include #Page annotation in the "page" class.
So adding this on it resolved the issue.