type mismatch with #helper - java

I'm trying to show a form with the values but it's not working.
my action:
public static Result login() {
User user = new User();
user.name = "Murilo";
Form<User> userForm = form(User.class);
return ok(login.render(userForm.fill(user)));
}
and my html:
#(myForm : play.data.Form[models.User])
<!DOCTYPE html>
<html>
<head>
</head>
<body>
#helper.inputText(myForm("name"))
</body>
</html>
but when I access it, the following error throws:
type mismatch; found : play.data.Form.Field required: play.api.data.Field

As a addition to nico_ekito's good answer: I usually do not use #helper.. because it is long and not/less readable if your form starts to grow (more fields). So I do the following:
#(editForm:Form[User]
#*** IMPORTS ****#
#import helper._
#form(routes.Tasks.save(), 'class -> "form-horizontal") {
#inputText(editForm:Form("description()").....)
#inputArea(editForm:Form("description()").....)
}

In your template, it should be:
#(myForm : Form[User])
<!DOCTYPE html>
<html>
<head>
</head>
<body>
#helper.inputText(myForm("name"))
</body>
</html>

Related

Html5 web storage not displaying data in a second page

I am using webstorage to save data.When i get the data through form & save it in a variable (in this case 'one') and try to display in the same page , it does that correctly. But when i import the same '.js' file and try to display it in a different page, it doesn't work. How to use webstorage to retrieve the data in a different page ? Also, i don't want to use query string !
First page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="webstorageAPI.js"></script>
<link href="webstorageAPI.css" rel="stylesheet" type="text/css" />
</head>
<body>
<section id="leftbox">
<section id="leftbox">
<form action="seconddisplayfile.html" method="post">
<p>(key) One: <input type="text" id="one" /></p>
<p>(value)Two:<textarea id="two"></textarea></p>
<p><input type="submit" id="button" value="Save" /></p>
</form>
</section>
<section id="rightbox">
Nothing yet !
</section>
</section>
</body>
</html>
Second page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="webstorageAPI.js"></script>
<link href="webstorageAPI.css" rel="stylesheet" type="text/css" />
</head>
<body>
<section id="wbox">
Nothing yet!
</section>
</body>
</html>
Javascript code:
// JavaScript Document
function doFirst(){
var button = document.getElementById("button");
button.addEventListener('click',save,false);
}
function save(){
var one = document.getElementById("one").value; //but only contents is to be taken not label n all
var two = document.getElementById('two').value;
localStorage.setItem("one",two); //Store in key-value pair. Anytime you use this you use if by addressing the variable ie key name.
//Retriveing data n displaying stored data
display(one); //now u hav stored two so its time to use one
}
function display(one){
var rightbox = document.getElementById('rightbox'); //refer right box
var two= localStorage.getItem("one"); //Get in key-value pair
rightbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;
/*----Second page display-----*/
var wbox = document.getElementById("wbox");
wbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;*/
}
window.addEventListener('load',doFirst,false);
#charset "utf-8";
/* CSS Document */
#leftbox{
float:left;
padding:20px;
border:3px solid #F20B84;
}
#rightbox{
float:left;
width:250px;
margin-left:20px;
padding:20px;
border:3px solid #8E1783;
}
Your second page doen't contain any element with id rightbox.so
var rightbox = document.getElementById('rightbox');
will return null.and when try to set innerHTML property of rightbox,js will throw
Uncaught TypeError: Cannot read property 'innerHTML' of null
so change your display to
function display(one){
var rightbox = document.getElementById('rightbox'); //refer right box
var two= localStorage.getItem("one"); //Get in key-value pair
if(rightbox)
rightbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;
/*----Second page display-----*/
var wbox = document.getElementById("wbox");
wbox.innerHTML= "Name of variable:"+one+"<br/>Value:"+two;*/
}
You have to call display in second page .

Thymeleaf didn't see objects from Spring

My template do not see objects, passed from Spring.
My code:
public class PublicModelAndView extends ModelAndView {
#Autowired
TemplateModulesHandler templateModulesHandler;
public void init() {
setViewName("index");
CSSProcessor cSSProcessor = new CSSProcessor();
cSSProcessor.setSiteRegion("public");
super.addObject("CSSProcessor", cSSProcessor);
JSProcessor jSProcessor = new JSProcessor();
super.addObject("JSProcessor", jSProcessor);
templateModulesHandler.setPublicModelAndView(this);
}
}
Contoller's code:
#SpringBootApplication
#Controller
public class IndexPage {
#Autowired
PublicModelAndView publicModelAndView;
#Autowired
OurServicesBean ourServicesBean;
#Autowired
PortfolioBean portfolioBean;
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView indexPage() {
publicModelAndView.setTemplate("publicSiteIndexPage");
publicModelAndView.addObject("pageTitle", "TITLE!!!!!!!!!!!!!!");
publicModelAndView.addObject("ourServices", ourServicesBean.getMenu());
publicModelAndView.addObject("portfolioWorkTypes", portfolioBean.getWorkTypes());
publicModelAndView.addObject("portfolioWorks", portfolioBean.getWorks());
return publicModelAndView;
}
}
Main template's code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
>
<head th:include="headerAndFooter/fragments/header :: publicSiteHeader">
<title></title>
</head>
<body>
hello!
</body>
</html>
Fragment's code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:fragment="publicSiteHeader">
<title>${pageTitle}</title>
<!--[if lte IE 8]>
<script src="<?= SITE_TEMPLATE_PATH ?>/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
</body>
</html>
As result I do not see value of the object pageTitle, but I see in page output code like
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>${pageTitle}</title>
Why thymeleaf didn't paste value of the pageTitle to between title tag's open and close?
The same code works good with JSP, but do not work with thymeleaf.
Thymeleaf is not JSP, so that's why your template does not work as you expect.
Look here http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-texts and use something like:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<title th:text="#{pageTitle}">page title</title>
Edited - my solution is for localised texts which is good practice anyway. if you want to use content of variable than use $.

eclipse is showing error on render() in play2

i am new on Play Framework and i am trying to develop sample java application
i have created new html page test.html
in controllers it is working if we simply return result in the form of string i.e. return ok("hello world") but it simply format all the styling/text and just shows "hello world" on the UI.
public static Result test() {
return ok("hello world");
}
// working fine
but it gives errors when i try this
public static Result test(){
return ok(test.render());
}
// giving error
it gives following errors
[error] /opt/ahsen/play-2.2.3/testapp/app/controllers/Application.java:15: render(java.lang.String,play.api.templates.Html) in views.html.test cannot be applied to ()
[error] return ok(test.render());
[error] (compile:compile) javac returned nonzero exit code
here is my test.html file
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.at("images/favicon.png")">
<script src="#routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript">
</script>
</head>
<body>
#content
</body>
</html>
help please
Your test.html template requires two parameters
#(title: String)(content: Html)
So you need to pass them to the template in your controller
public static Result test(){
String title = "test title";
Html content = // create content
return ok(test.render(title, content));
}
Better solution
Test.html looks rather as a more general layout template. I would create a separate file for generating content instead passing it directly from a controller.
content.html
#(title: String)
#test(title){
<span>this is the content</span>
}
And in the controller it would look like this.
public static Result test(){
String title = "test title";
return ok(content.render(title));
}

HTTP Status 500 - file:/survey.jsp(16,15) jsp:getProperty for bean with name 'survey'. Name was not previously introduced as per JSP.5.3

Hi I am new to JSP and JavaBean. I am practicing by writing an application where multiple pages will share a javabean component. The page "check.jsp" instantiates the bean and sets a property without any error. But whenever I try to getProperty in another jsp, survey.jsp, I get the error:
HTTP Status 500 - file:/survey.jsp(16,15) jsp:getProperty for bean with name 'survey'. Name was not previously introduced as per JSP.5.3
I have double checked that the name in the get and set properties are exactly the same as my bean id in the action element. Please I need help
CHECK.jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<jsp:useBean id="survey" scope="application" class="appScope.SurveyBean"/>
<jsp:setProperty name="survey" property="quantity" value='<%= request.getParameter("title")%>' />
<form action="/appScope/survey.jsp" method="POST">
<h3> Thanks for your input</h3>
<h3> Please check the survey summary status if you want</h3><br/>
<input type="submit" value="Check Status" />
</form>
</body>
</html>
This is my javabean: SurveyBean.java:
package appScope;
public class SurveyBean {
private int javaQuantity = 0;
private int csQuantity = 0;
public int getJavaQuantity()
{
return javaQuantity;
}
public int getCsQuantity()
{
return csQuantity;
}
public void setQuantity(String bookTitle)
{
try
{
if(bookTitle.equals("java"))
{
javaQuantity++;
}
if (bookTitle.equals("c"))
{
csQuantity++;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
and here is survey.jsp where i get the error:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1>Survey Summary</h1>
//ERROR IS HERE
Java = <jsp:getProperty name="survey" property="javaQuantity" /> <br/>
C# = <jsp:getProperty name="survey" property="csQuantity" />
</body>
</html>
Name was not previously introduced
This indicates that you haven't told your JSP about this bean as of yet. You are directly using the <jsp:getProperty> before letting the JSP know about the bean.
You need to use the <jsp:useBean> tag to define the bean in the survey.jsp.The name attribute of the getProperty tag must match the id attribute of the useBean tag :
<jsp:useBean id="survey" class="appScope.SurveyBean" scope="request">
But this won't work , unless you set the bean named survey in the request scope in the check.jsp file and post that request to the survey.jsp.

Output java variable value to be parsed in JS

I have this code:
<script type="text/javascript">
var endpoint = "http://localhost:8080/LWP/in.jsp?code=" + "<% out.println(request.getParameter("code")); %>";
window.opener.location.href = endpoint;
window.close();
</script>
What I expect it to do is redirect the page which opened the browser window this is processing in to
http://localhost:8080/LWP/in.jsp?code=<code here>
If I remove the <% out.println() %> portion of the script, it works fine, and I am redirected as expected (minus the value being passed in).
What am I doing incorrectly with the output of the parameter?
I also tried removing out.println. Still does not work.
Figured it out. Had to assign the value to a new variable, then use that.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<% String code = request.getParameter("code"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
var code = "<%=code%>";
var endpoint = "http://localhost:8080/LWP/in.jsp?code=" + code;
window.opener.location.href = endpoint;
window.close();
</script>
</head>
<body>
</body>
</html>

Categories