I have list.add(int,String) in action class. I am getting this list in my jsp page as
<s:iterator value="list" var="emc" status="status">
<s:checkbox name="selectedCoverage" fieldValue="%{#emc.dataId}" />
<s:label value="%{#emc.PersonName}" />
</s:iterator>
Now I have three checkboxes, User did not selected any checkbox and hit submit which should throw validation errors. I have getters and setters for this check box in action class.
When I debug the code and see the value of selectedCoverage it is displaying me ognl.NoConversionPossibe instead of null. What is the issue and where am I going wrong?
Related
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.
I have created a from in JSP page name add.jsp to save data like this
<s:form action="AddDomain">
<s:push value="idp">
<s:textfield name="domainName" label="Domain Name" />
<s:textfield name="url" label="Domain URL" />
<s:textfield name="noOfLicense" label="License Purchased" />
<s:textfield name="licenseExpireDate" label="License Expire Date" title="YYYY-MM-DD like 2013-01-21" />
<s:textfield name="userActiveDuration" label="Active User Duration"
title="please mention in days" />
<s:textarea cols="30" rows="5" name="notes" label="Note"></s:textarea>
<s:submit value="Add"></s:submit>
</s:push>
</s:form>
Action Method that show this view is as
public String addDomainPage() {
return ActionSupport.SUCCESS;
}
I have created another page that list the all domains and provide a edit link to edit any domain. When Use click on edit URL this action is called
public String loadDomain() {
HttpServletRequest request = ServletActionContext.getRequest();
String url = request.getParameter("durl");
IDPBroker broker = new IDPBroker();
idp = broker.getDomainByURL(url);
return ActionSupport.SUCCESS;
}
On successful completion of action I show the add.jsp page. Struts populate the data in JSP page.
Now, issue is that I want to change the value of action attribute of form tag. I also want to change the value of submit button to 'Edit'. I have plan to create some private attribute(action,Label) in Action class and when an addDomainPage action is call I will change the value of these attribute with respect to add page. Similar for loadDomain action. Now I don't know how to do this means how to use these private attributes in view. Tell me is I am doing correctly and what to do next?
The same action class could be used to map different methods on submit buttons. Like
<s:submit value="Add" method="addDomainPage" />
<s:submit value="Load" method="loadDomain" />
The form action attribute should map to the action class execute method which will never call if you use submit buttons like that. The DMI which is enabled by default allows to call specified methods.
If you want to dynamically change attributes in the Struts tags you could use OGNL expressions in JSP instead of hardcoded values. For this purpose you should define properties in the action that define dynamic values before result is executed. For example
public String getAction(){
return "AddDomain";
}
<s:form action="%{action}">
I am using Struts2.
<s:iterator value="empReportFields" var="empReportField"
<s:select name="%{#empReportField.fieldName}" list="%{#empReportField.listName}" listKey="id" listValue="name" cssClass="search" headerValue="All" headerKey="All" />
<s:property value="#empReportField.listName" />
// Here it is displaying proper list name
</s:iterator>
I am fetching out these data from my db. Now I am displaying specific list in select box (<s:select list="<ListName>" />) which is stored in column of my table (Database).
Normally it runs like.
<s:select name="emp" list="locationList"
listKey="id" listValue="name"
headerValue="All" headerKey="All" />
It will work well.
But I find simple select box with no list value in it. So what is the actual problem??
In short I want to call list dynamically.
The s:select tag itslef has a list attribute where you can directly give the name of the list (in action class) you want to fill the dropdown with. You do not need an iterator for packing values into s:select dropdown.
Try this:
<s:select label="Select from here"
headerKey="-1" headerValue="Select"
list="listNameHere"
name="feildNameHere" />
Here 'listNameHere' is the list in action layer and the 'feildNameHere' is a instance variable in action class which recieves the value selected by the user.
According to how I understand the question you want to load <s:select list dynamically according to the iterator value.
If so just use the <s:select list like this <s:select list="#empReportField.listName".
Where <s:iterator value="empReportFields" is a list and inside that list another list or map named listName.
I have created a struts2 login page, wherein I have all basic components necessary for login. I'm using struts2 text tag for a label. Below is my login page code snippet.
<body>
<h2>Demo - Login</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label_username" size="20" />
<s:password name="password" key="label_password" size="20" />
<s:submit name="signIn" key="label_login" align="center" />
<s:text name="name_msg"/>
<s:submit name="signUp" key="label_signUp"></s:submit>
</s:form>
</body>
I always see that text(New to Demo ?) is displayed after heading, as shown in below Image. There text is read from MessageBundle. I tried by giving some direct text value, despite of referring to resource bundle, even though same result. Where I was wrong.
The <s:text> tag displays text with no "decoration".
The default "xhtml" theme's form tags emit table markup.
This means you're currently generating invalid HTML, so the text will show up in essentially arbitrary locations based on how the browser handles stuff showing up in between table rows.
Viewing the source would have answered this question immediately.
You need to either put the text into a table row, as with everything else in the form tag, or use the "simple" theme and do all the layout, error messages, etc. yourself.
When using Struts2 validation, when you put the <s:actionerror> tag in your JSP, the default behavior is to display all the action errors at that point in the page.
Is there a way to display only specific error messages at that point? For example, in the case of fielderror one only needs to add the fieldName attribute. Is there an attribute of actionerror that accomplishes similar behavior?
For field specific error the function is : hasFieldErrors()
You can use it like this:
<s:if test="hasFieldErrors()">
<div class="fieldErrors">
<!-- iterate through the fields errors, customize what you need -->
<s:iterator value="fieldErrors">
<s:property value="key"/>:
<s:iterator value="value">
<s:property/>
</s:iterator>
</s:iterator>
</div>
</s:if>
References:
Struts 2 documentation.
Interesting reading:
Why are my actionErrors and fieldErrors displayed with braces []?
https://www.mkyong.com/struts2/working-with-struts-2-theme-template/