I can not find the variable thymeleaf. Why? - thymeleaf

I can not find variable for building simple for-each-loop in Thymeleaf template.
<div th:each="md:${mds}">
<a th:text="${md}"></a>
</div>
Cannot resolve 'md' by IDEA.
Here is my server code:
#GetMapping("/example.html")
private String mds2book(ModelMap model) throws IOException {
BookConfig bookConfig = Util.readBookConfig(null);
List<Md> mds = Util.readMds();
model.addAttribute("mds", mds);
return "mds2book";
}
If you know where the error is, I think, I need your help.
Thanks in advance.

Adding html header:
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
Success!
Here is answer.

Related

ASP.Net Core 3.1 SelectList load wrong data from ViewData

I am trying to load a SelectList through ViewData. Previously, I have loaded ViewData["Almacenes"] with a List. But, when view is showed, there is not the expected data on SelectList. If I inspect the element in chrome, it shows me wrong data loaded. Could anyone please help me? I will really appreciate it. Thanks.
Repository
public interface ISga_eDataService
{
public List<SelectListItem> GetAlmacenes();
}
Controller
public IActionResult Index()
{
ViewData["almacenes"] = _dataService.GetAlmacenes();
return View();
}
View
<div class="text-center">
<span>Almacenes</span>
<select id="Almacenes" asp-items="(IEnumerable<SelectListItem>)#ViewData["almacenes"]" />
</div>
Inspecting SelectList in Chrome
<select id="Almacenes">
© 2020 - Sga_eMinMaxChecker - Privacy
<script src="/lib/jquery/dist/jquery.min.js"></script>
<script src="/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0"></script>
</select>
Inmediate Window
?#ViewData["almacenes"]
Count = 32
[0]: {Microsoft.AspNetCore.Mvc.Rendering.SelectListItem}
[1]: {Microsoft.AspNetCore.Mvc.Rendering.SelectListItem}
[2]: {Microsoft.AspNetCore.Mvc.Rendering.SelectListItem}
[3]: {Microsoft.AspNetCore.Mvc.Rendering.SelectListItem}
[4]: {Microsoft.AspNetCore.Mvc.Rendering.SelectListItem}
...
I have same problem, and I solve this with send ViewData to View as selectList.
you can use this in your Controller:
var AlmacenesList = _dataService.GetAlmacenes();
List<SelectListItem> Almacenes= new List<SelectListItem>();
provinces.AddRange(AlmacenesList);
ViewData["Almacenes"] = new SelectList(Almacenes, "Value", "Text");
and View:
<select asp-items="#(ViewData["Almacenes"] as SelectList)"></select>
I have found a solution, but I still don't understand why the control behaved like that. So, if any could explain me why, I'll really appreciate it.
I have changed from
<select id="Almacenes" asp-items="(IEnumerable<SelectListItem>)#ViewData["almacenes"]" />
To
<select id="Almacenes" asp-items="(IEnumerable<SelectListItem>)#ViewData["almacenes"]"></select>
and it works.

prevent groovy from changing " to "

I have an XML that has a tag value like the following:
<ProjectNote>
<Note><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.3790.4944" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<P>Key Deliverables</P>
<UL>
<LI>schedule development
<LI>scope development (SOW)
<LI>business case (depending on project)
<LI>contracts (who will be used)
<LI>overall budget
<LI>Assign Key Stakeholders
<LI>Sitewalks and PreCon Meetings
<LI>Need Clearance?</LI></UL>
<P>&nbsp;</P></BODY></HTML>
</Note>
<ProjectNote>
I am reading this file with groovy script and making some changes to it and writing it back to the file. However, the " is getting converted to " while parsing the file with XmlSluper. I don't want to change any other section of the file other than adding a new nodeto it. How can I keep the file as it is?
I am using following code:
package test
import groovy.xml.*
/**
* A Simple Example that searches information from XML parsed by XmlSlurper.
*/
class Test {
static srcXMLPath = "C:/SRC_Project/628548_C453_Original.xml"
static updXMLPath = "C:/SRC_Project/628548_C453_Updated.xml"
static def writer
static main(args) {
File srcFile = new File(srcXMLPath)
def baseXMLStr = new XmlSlurper(false,false).parse(srcFile)
def newXMLStr = new groovy.xml.StreamingMarkupBuilder().bind {
List_Wrapper {
mkp.yield baseXMLStr
}
}
writer = new FileWriter(updXMLPath)
groovy.xml.XmlUtil.serialize( newXMLStr,writer )
writer.close()
}
}
However the updated file gets changed to this which is not my intention:
<ProjectNote>
<Note><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.3790.4944" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<P>Key Deliverables</P>
<UL>
<LI>As Builts (if needed)
<UL>
<LI>Mapping &amp; Design Drawings</LI></UL>
<LI>Engineer needs final approval
<P>&nbsp;</P></BODY></HTML>
</Note>
<ProjectNote>
Could someone let me know how to avoid it. it is clearly not changing other escape characters
You may fix it after building markup:
new File(updXMLPath) << XmlUtil.serialize(newXMLStr).replaceAll('"', '"')
But if you want to add only wrapper, then you don't need to parse source xml, you may past source file to markup as it:
def newXMLStr = new StreamingMarkupBuilder().bind {
List_Wrapper {
mkp.yieldUnescaped srcFile.text
}
}
Finally if you need to put only one List_Wrapper tag, maybe better to do:
new File(updXMLPath) << "<List_Wrapper>${new File(srcXMLPath).text}</List_Wrapper>"

window.location.href not working in phonegap

I want to redirect to another page in Phonegap.
I have written the following code in javascript but it is not getting redirected:
window.location.href = "http://www.google.com";
Can anyone advise why it is not working?
You have to allow navigation of external sites inside your application by configuring the whitelist.
You have to use allow-navigation tag like this:
<allow-navigation href="http://www.google.com/*" />
Try doing the following:
Open your file Cordova.plist file
Right click on ExternalHosts -> Add Row
Set the String value of the new added row to *.
So, you should have your new added row like this:
Item0 String *
Normally, you should replace * with the external URL that you want to provide access to (like http://www.google.com for instance), but I used * to make sure that the problem comes from there or not.
For more information, check the "Domain Whitelist Guide" section of the online doc: http://docs.phonegap.com/en/2.1.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide .
Here's a simple working example using window.location.href:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" charset="utf-8">
function init() {
window.location.href = "http://www.google.com";
}
</script>
</head>
<body onload="init();">
</body>
</html>
Let me know if this works.
Most likely it's the page you are moving to. Does that page have the phongap.js files in it etc?
Try a simple test: create a new HTML page with just the basic elements and a couple words in the body so you know you are there. Save it as test.html. Now try window.location="test.html".
If that works then you know it's something in the new page.
Good luck!
Have you checked your framework initializations? Make sure jquery and phonegap are completely loaded before you try to change the page. Or else phonegap will hang and break.
Take a look here: Correct way of using JQuery-Mobile/Phonegap together?
Working fine for me with Cordova 4. Can you try remote debugging with Google Chrome and see what happen in the Javascript console?
Try without href:
window.location = "http://www.google.com";
if you use like window.location or window.location.href and it stll doesn't work in IOS or safrai.
you can use this:
var isAndroid = !!navigator.userAgent.match(/android/ig);
var targetUrl = 'your url';
window.location = targetUrl;
if(!isAndroid) {
var doc = window.document,
ifr = doc.createElement('iframe');
ifr.src = targetUrl;
ifr.style.cssText = 'display:none;';
doc.body.appendChild(ifr);
}

How to set a web page as PhoneGap startPage?

I began to learn PhoneGap recently in iOS platform, and I encountered a problem. How can I change the startPage?
I set startPage as follows:
self.viewController.startPage = #"http://www.google.com";
It did not work, and I also tried the following way:
NSURLRequest *request =
[NSURLRequest requestWithURL:[URL URLWithString:#"http://www.google.com"]];
It also did not work.
I do not know how to do this. Is there anyone can help me out?
Here is what I did for my application.
Create a local index.html page as phonegap expects it.
Inside html, set window.location to whatever page you need it to go to.
Here is my index.html:
<html>
<head>
<script> window.location.href='http://www.google.com' </script>
</head>
<body>
<h1 class='loading'> Loading </h1>
</body>
</html>
Note: Make sure you put the http:// in the location otherwise it may consider as relative link.
Try this,
......
......
public partial class MainPage : PhoneApplicationPage
{
//variables…..
public MainPage()
{
InitializeComponent();
PGView.StartPageUri = new Uri(“www.google.com“, UriKind.Relative);
.....
.....
}
Check How to change start page in PhoneGap 1.xstrong text
You can put starting point of application in plist file.

What is the equivalent to Page.ResolveUrl in ASP.NET MVC?

What is the equivalent to Page.ResolveUrl in ASP.NET MVC available in the Controller?
It is Url.Content:
ASPX:
<link rel="stylesheet" href="<%= Url.Content("~/Content/style.css") %>" type="text/css" />
Razor:
<link rel="stylesheet" href="#Url.Content("~/Content/style.css")" type="text/css" />
This should do what you're looking for...
System.Web.VirtualPathUtility.ToAbsolute("~/")
Here are a whole bunch of ways to resolve a path that uses that application root operator (~)
UrlHelper.Content
HttpServerUtility.MapPath
WebPageExecutingBase.Href
VirtualPathUtility.ToAbsolute
Control.ResolveUrl
To call any method with inline code on an asp.net page, the method either needs to be exposed as an instance variable on the current object, or available as a static/shared method.
A typical MVC page gives us access to quite a few of these as properties via the WebViewPage. Ever wonder when you type #ViewData, you get magically wired up to the ViewData? That's because you have hit a property exposed by the MVC page you're on.
So to call these methods, we don't necessarily refer to the type they represent, but the instance property that exposes them.
We can call the above instance methods like this (respectively):
href="#Url.Content("~/index.html")"
href="#Server.MapPath("~/index.html")"
href="#Href("~/index.html")"
We can do this to call a shared method that doesn't need an instance:
href="#VirtualPathUtility.ToAbsolute("~/index.html")"
AFAIK, an MVC page doesn't automatically create an instance of anything from the System.Web.UI namespace, from which ResolveUrl inherits. If, for some reason, you really wanted to use that particular method, you could just new up a control and use the methods it exposes, but I would highly recommend against it.
#Code
Dim newControl As New System.Web.UI.Control
Dim resolvedUrl = newControl.ResolveUrl("~/index.html")
End Code
href="#resolvedUrl"
That all said, I would recommend using #Url.Content as it fits best with MVC paradigms
UrlHelper.Content() does the same thing as Control.ResolveUrl().
For Further References:
http://stephenwalther.com/archive/2009/02/18/asp-net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx
You don't need to do this anymore in Razor v2.0/ASP.NET MVC 4.
Just use the "~" in a razor page and it will resolve it for you.
<link rel="stylesheet" href="~/Content/style.css" type="text/css" />
Source
In my case, I find #Href not being enough in the way it deals with query strings in a URL. I prefer to wrap it inside the Raw method:
<script>
var isKendoWindow = false;
var myTimeOut;
clearTimeout(myTimeOut);
var sessionTimeout = (#Session.Timeout * 60000) - 5;
function doRedirect() {
if (!isKendoWindow)
window.location.href = '#Html.Raw(Href("~/Logon.aspx?brandid=" + SessionController.LandingBrandId + "&errCode=5055"))';
}
myTimeOut = setTimeout('doRedirect()', sessionTimeout);
</script>
Or you can create your own version like this:
public static IHtmlString ResolveUrl(this HtmlHelper htmlHelper, string url)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
return htmlHelper.Raw(urlHelper.Content(url));
}
Another way to solve this issue:
Resolve the url in a code block at the top of the page or in code behind.
#page
#model IndexModel
#{
ViewData["Title"] = "Home page";
Layout = "~/Pages/Shared/_IndexLayout.cshtml";
String img1 = Url.Content("~/img/people11.jpg");
}
Then use the variable in the html.
<div class="col-12 col-lg-8" style="background-image: url('#img1');"> </div>
Server.MapPath() //returna full path
or
url.content()
try using Server.MapPath().

Resources