I have a variable that is obtain inside each block in thymeleaf. I want to send that variable to a certain method in my controller. The limitation is that the variable is obtain inside a block which makes it local, not accessible global.Hence I get an error while trying to use it. How can I move inside the scope to get the variable so as I can use it global in thymeleaf.
<form th:action="#{/masomo/somo(date=${dateMpya.date})}" method="POST">
<select id="date" name="date" required="true">
<option value="none" selected disabled hidden >
Select a Date
</option>
<th:block th:each="somoChagua : ${masomoChagua}">
<option th:each="dateMpya: ${somoChagua}" th:value="${dateMpya}" th:text="${dateMpya.date}" ></option>
</th:block>
</select>
<button type="submit"><i class="fa fa-search"></i> </button>
</form>
There can be many different "dateMpya" objects for each "somoChagua".
But there is only one submit button.
So which "dateMpya" should be used for the submit button value?
I think what you are actually trying to do here is get the value of the "dateMpya" which the user selected in the drop-down. Is that right?
If that's the case there is no need to add any attributes to the submit button. You would access that value by using the name of the select element, which is "date".
EDIT: For the same reason you also need to remove the (date=${dateMpya.date}) part of the form action as well. The value selected in the drop-down will automatically be submitted under the name of the select element "date", it does not need to be specified.
Related
I have been doing school's project recently and started learning Java Web/Spring/Bootstrap and stuff only a week ago, so please do forgive and correct me if I got any idea wrong.
So I was working on some webpage following an online tutorial and it's really great that I can fetch user's input by using Thymeleaf tags like codes below
<div class="ip input-group" align="center">
<input id="username" type="text" class="form-control" name="username" placeholder="Username" th:value="*{username}"/>
</div>
Though I'm having a hard time trying to fetch input from select box or radio button like codes below (am I doing anything wrong here?)
<div class="input-group">
<select class="btn btn-default" th:value="*{sex}">
<option>Male</option>
<option>Fmale</option>
<option>Other</option>
</select>
</div>
Since it's able to fetch input from simple input area, I'm thinking that there should be a way to acquire input or data from select box or radio button by using Thymeleaf?
th:field tag should be in <select> tag, but it does not exist.
th:value tag should be in <option> tag, not in <select> tag.
As described in thymeleaf docs:
Select fields have two parts: the tag and its nested
tags. When creating this kind of field, only the tag has to
include a th:field attribute, but the th:value attributes in the
nested tags will be very important because they will provide
the means of knowing which is the currently selected option (in a
similar way to non-boolean checkboxes and radio buttons).
Code snipped in source
I have a parent jsp file that includes two child jsp's. I have a variable defined in the parent file like so:
<c:set var="test" value="N" />
which I then pass into two child jsp files:
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp">
<jsp:param name="controlFlag" value="${test}"/>
</jsp:include>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp">
<jsp:param name="controlFlag" value="${test}"/>
</jsp:include>
</div>
In my page1.jsp file I then store the value in a hidden div:
<div id="cashAuditFlag" style="display: none;">${param.cashAuditFlag}</div>
I then have a button in page1.jsp that, when clicked, I want it to change the value of the parent variable ${test} to "Y". This in turn would then change the value of ${test} in page2.jsp which would cause a change in page2.jsp.
I basically want to have a child jsp communicate an update to another child jsp, both of which belong to the same parent.
A - Is this the best way of doing this process?
B - How can I have a child jsp update the parent jsp variable?
Thank you!
To understand this, you need to understand scopes. Think of a scope as a bucket that variables go into when they are defined. Some code only has access to some of those buckets.
Request scoped variables are available in any part of the code that knows about the http request. There is only one request scope per HTTP request to your webapp. In your example, the parent file, page1.jsp, and page2.jsp all have access to that request scope. For example, if you did this in your parent JSP page:
<c:set var="test" value="N" scope="request" />
...it would put the "test" variable into the request scope bucket with a value of N.
Then, if you want to view or modify this value in either page1.jsp or page2.jsp, you don't even need to have a jsp:param element in your jsp:include, so you can just do this:
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp"/>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp"/>
</div>
So, if you want to display this in either child page, you can simply use Expression Language and tell it to look in the requestScope for the variable named "test" by using the requestScope object:
<p>The Test Variable is: ${requestScope.test}</p>
Similarly, if you wanted to modify this variable in either childPage, you can simply do another c:set statement:
<c:set var="test" value="Y" scope="request" />
Now, if you print out the value of ${requestScope.test} in any page, it will be Y.
In your example, when you used the c:set statement without scope="request", you created a variable in that jsp page's "page scope", meaning you could only access that variable in the jsp code you wrote in your parent jsp page.
Now, as to whether this is the best way to do this...
You say you have a button in page1.jsp that, when clicked, should change the test variable to Y and cause some display change in page2.jsp. Here's the flow of what would need to happen:
User vists your JSP page at some url, like "mywebapp/testPage.jsp"
The page renders. The initial c:set statement runs which sets the "test" var to a value of "N".
The user clicks the button, which causes the browser to send a new request but adds a request parameter, resulting in a request of something like "mywebapp/testPage.jsp?buttonClicked=1"
All your JSP pages render again (keep in mind JSP does not do things "dynamically" i.e. without a browser refresh - when you click a button, the browser sends a new HTTP request to the webserver and your JSP is rendered again).
At the top of your parent JSP, you need logic that checks whether the buttonClicked request parameter is present. If so, it sets the value of test to "Y" instead of "N".
So, to explain: in order to have the page render differently based on whether the button was just clicked, you would need to have your button pass a request parameter when it is clicked, and you would need to have your JSP look at the new request to find that request parameter (to see the value that was submitted when the button was clicked). If you don't have your code check that, then, every time your page loads, your parent JSP page will just keep re-setting the test variable to N because your initial c:set value="N" statement will always run when the JSP renders.
So, if you want something like the case you described, you'd have to do something like this in your parent JSP page:
<c:set var="test" value="N" scope="request"/>
<!-- Here's the check for whether the request parameter is present -->
<c:if test="${not empty param.buttonClicked}">
<c:set var="test" value="Y" scope="request"/>
</c:if>
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp"/>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp"/>
</div>
...then in page1.jsp, where you have the button, you would do something like this:
<input type="submit" name="buttonClicked" value="1" />
That way, when the button is clicked, it will refresh the page and add a new request parameter called buttonClicked with a value of "1". In the parent JSP, it will see that this request parameter is present (with "not empty param.buttonClicked") and it will set the value of the test variable to "Y", overwriting the previous assignment of "N".
Then in your page2.jsp, you can access the value of test using ${requestScope.test} at any point on your page.
I want to pass values of two variables when a link is clicked to another page I am using query parameter but I am only able to send one variable through it. I know about session.setAttribute() but don't know how can I use it based upon links...Foreg:
<p> < </p>
<a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value);%>" class="classname1" > > </a>
This is my code I know its wrong..I just want is If I click on first link than value1 should be passed and If I click on 2nd link value should be passed.P.S.:I have already passes search variable through query parameter but now If I try to pass second parameter through session only the final value i.e second initialized value only counts? what to do?
EDIT:
suppose my code is this:
<form class="navbar-form navbar-right" action="Search.jsp" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
Here one variable search is passes through form How can I pass another variable value?should it be like:
<form class="navbar-form navbar-right" action="Search.jsp?item1=<%=value%>" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
You can send multiple parameters like,
href="Search.jsp?item=<%=search%>&item2=value2&item3=value3.."
Also to add <%session.setAttribute("val",value1);%> will be executed at server side irrespective of the click of the hyperlink.
For the form you can add another input parameter in the form,
<input type="text" name="item1" value="<%=value%>">
You need to add some separator in between two values e.g.#
And while reading at server side you can split those values based on that separator
You may try using this example to send multiple values:
With same name
With diff name
Separate the two values by using &:
<a href="Search.jsp?item=<%=search%>&item2=<%=value2%>">
On your search.jsp fetch values as :
request.getParameter("item");
request.getParameter("item2");
You need to call session.getAttribute in the place where you are calling session.setAttribute and session.setAttribute should be called in controller or in the jsp before the link tag to set the value. Please separate values like Search.jsp?item1=value1&item2=value2
<td>Branch</td>
<td>
<select name="branch" type="text">
<option value="0">Select Branch</option>
<option value="CE">CE</option>
<option value="IT">IT</option>
</td>
<td>Batch</td>
i.e if i have branch field in my jsp form
so my branch firld contains 2 inputs as 1.CE & 2.IT.
So when i click on CE ,so it have to show me A1,A2,....A5 & when i click on IT, it will have to show B1...B5 from which we can select any one of the field.
So, pls help me for above program.
Here is my code.
Pls help me what code should i write in "Batch" field""
you have to register onChange() event of the branch and call an javascript function which will populate the batch field accordingly.
i am using spring mvc,
<form:select class="form-control" id ="cmsphyexamtesttype_cmsPhysicalExamCategory_id" path="cmsPhysicalExamCategory.id">
<form:option value="0" label="--- Please select the Category ---"/>
<form:options items="${cmsphyexamtestcategorys}" itemLabel="name" itemValue="id" />
</form:select>
html code
<select id="cmsphyexamtesttype_cmsPhysicalExamCategory_id" name="cmsPhysicalExamCategory.id" class="form-control">
<option value="0">--- Please select the Category ---</option>
<option value="2">Genaral</option><option value="3">EYE</option><option value="4">HENT</option><option value="5">CHEST</option>
</select>
this one is working fine with new form , but in edit mode,it is not working do you have any idea it gives selected value while rendered to editing mode, i am new to spring mvc, is there any thing wrong with this code?
You are path binding the drop down value to the
cmsPhysicalExamCategory.id
So, the selected value will be retained in the form when you recall the form again for some other operation like 'edit.
You can change the value in the drop down and the new value will be path binded to the model. Here, its not getting binded. It may be because of the error in path bind/form submit.
Please post the form submit code/controller code for more help.
From your question what I get is that you have drop down and on submitting again , you want to render the same form, you are getting the change value but drop down value still persist to the default , first attribute of drop down, and you want to have the attribute same as value, in case this is the elaborated issue, you need to explicitly check in list for what value you want to select and set the attribute as Selected.