Checkboxes using struts - struts

Hi I am willing to update the value of the checkbox , fetched from the Database, loaded on the JSP. I am creating a Employee Profile. The Jsp has fields Employee Name, Employee Address, Employee technical Skills.
The Employee Skills has following Checkboxes to select the following values
Checkbox1: Java Checkbox2: Dot net
For a Employee X, Does not have both the computational skills Java and Dot net in the database. I am able to fetch the record from the database to the JSP.
In the action class I am fetching the values from the database and setting them into the EmployeeForm which has the two getters and setters. I am setting these values in request also with setattribute.
In the EmployeeUpdate.jsp I am doing the following
String skillsValue1=(String)request.getAttribute("C/C++");
String skillsValue2=(String)request.getAttribute("Java");
<%if(skillsValue1!=null){ %>
<html:checkbox property="ComputationalSkill1" value="C/C++"/>
<%}else{ %>
<html:checkbox property="ComputationalSkill1" value=" "/>
<%}%>
</td>
<%if(skillsValue2!=null){ %>
<html:checkbox property="ComputationalSkill2" value="Java"/>
<%}else{ %>
<html:checkbox property="ComputationalSkill2" value=" "/>
<%}%>
</td>
The following code displays the checkboxes perfectly for the employees with any of the above skills and without skills.
I am getting the following problem here
When I uncheck the value on any field, which was checked earlier , the value is setting to null in the bean perfectly.
But when When I Check the value on any field, which was not checked earlier(to update any of the fields), There is no value set in the bean for the particular property, neither null also.
Could anyone let me know how to get the changed values of a checkbox, i,e when I change the state from uncheck to check state, the value should be set in the property.

If an employee has the skill Java, you're displaying the following checkbox:
<html:checkbox property="ComputationalSkill2" value="Java"/>
This means that on submit, if the checkbox is checked, the following parameter will be sent:
ComputationalSkill2=Java
If an employee doesn't have the skill Java, you're displaying the following checkbox:
<html:checkbox property="ComputationalSkill2" value=" "/>
This means that on submit, if the checkbox is checked, the following parameter will be sent:
ComputationalSkill2=<blank space>
What you want is generate always the same checkbox (the first one), but preselect it if the employee has the skill Java, and not preselect it if the employee doesn't have the skill Java.
This is not possible as is using the <html:checkbox> tag, because it's supposed to be bound to a boolean property. So you could instead have a property isJavaSkilled()/setJavaSkilled() in your form bean, and use
<html:checkbox property="javaSkilled"/>
Struts will pre-select the checkbox if the form's javaSkilled property is true, and leave it unchecked if it's false.
Note however, that since you have a list of skills that can be or not present, you should instead have a property getSkills()/setSkills() of type String[], and use a <html:multibox> tag.

Related

Dynamic Form in Vue - checkbox stays checked

I have a select input. and checkbox-list in vuejs form. Checkbox list depend on what is chosen in select. The problem is that if i check let's say 1 or 2 checkboxes, after I change select, 1 or 2 checkboxes will always stay checked despite the fact that values and labels of checkboxes have already changed. It looks like it doesn't build new checkboxes with new 'checked' attribute.
Select has onChangefunction() in which I change list of items to be checked in checkbox list. Checked() function in checkbox checks whether this exact element should be checked and returns true or false
<select #change="onChange" ...
<input type="checkbox" :checked="checked()" ...
The thing is that even if checked() function will always just return false, the checkbox will stay checked after I manually checked it on the page however many time I will change select input choice
To really give a good answer, I think a bit more information might be required. That being said, it looks like you're binding is incorrect. Your input line probably should be:
<input type="checkbox" :checked="checked" ...
So your checked function shouldn't have parens on it.
If that's not the issue, please post the rest of your component so we can see the script and data values.
Agreed with #DRich here but you can use #input method to call a method
<select #input="onChange" ...
I use this most of the time

How to display data in struts form if object has another object

I'm working on struts 1.3 framework application.
I have one object which I'm setting in request attribute.
request.setAttribute("school",school);
And I'm trying to display that object through <bean:define> tag.
E.g.
School is Value Object
School school;
in school VO object I have another two object
Student student;
Teacher teacher;
And I'm trying to display value of student as well as teacher object
<bean:define id="summary" name="school" />
<bean:define id="StudentSummary" name="summary" property="student"/>
<bean:define id="TeacherSummary" name="summary" property="teacher"/>
And writing this element through tag
<bean:write name="StudentSummary" property="name" />
<bean:write name="StudentSummary" property="class" />
<bean:write name="TeacherSummary" property="name" />
But it is giving
javax.servlet.jsp.JspException: Cannot find message resources under key org.apache.struts.action.MESSAGE
what would be wrong in the code.
I have never done it using bean tag but you can do it using Expression language(EL). EL, I believe is a more standard way to do things.
Take a look at this previous post. I think it helps Link
I think in your case you can do something along the line of
<c:out value="${school.student.name}"/>
The above statement will print the value of "name", if you have a "name" property in your student object.

options_from_collection_for_select Definition

I have just read the Rails API definition for option_from_collection_for_select.
Returns a string of option tags that have been compiled by iterating over the collection and assigning the result of a call to the value_method as the option value and the text_method as the option text
Now I am very new to rails so was wondering if someone could break this down into smaller chunks and explain what is happening, dumb it down if you will, the explanation is very high level (well for me at the moment)
Thank you
Using the example from the Ruby on Rails API, let's assume you have a Person model that has an id attribute and a name attribute.
Say you have a form that creates a new project and assigns it to a person. Maybe you want to have a drop down select for what person you're assigning this project to. You could use options_from_collection_for_select for something like this.
<%= f.label :person, "Assigned Person" %>
<%= f.select(:person, options_from_collection_for_select(#people, "id", "name") )
(f by the way would be referring to the #project variable for our example form here.)
What this would do is create an option in your select drop-down for each person contained in the instance variable #people. Each of the <option> tags would have the id of that person assigned to its value attribute, and the text for that option would be the person's name.
So if your #people variable contained [#<Person id: 1, name: "Brock Sampson">, #<Person id: 2, name: "Byron Orpheus">], you would get HTML output like this:
<label for="project_person">Assigned person"</label>
<select id="project_person" name="project[person]">
<option value="1">Brock Sampson</option>
<option value="2">Byron Orpheus</option>
</select>
Does that make more sense?

how to combine html:text and bean:write

I am using <bean:write/> tag in my struts program to catch my data into text field but I want to update/edit those data which is coming by this tag so I want to use <html:text/> tag with <bean:write/>, is it possible?
is there any other option to update <bean:write/> tag's data.
I am using this tag like -
<bean:write name="Customer" property="lastname"/>
It's not entirely clear to me what you want to update, and under what circumstances.
If you're saying to want to have the customer's last name be an editable text field, then initialize the ActionForm with the appropriate values before displaying it to the user; you don't need the <bean:write> tag at all if you're just trying to initialize/show a form field.

struts form and JSTL

How to access a form variable using JSTL ?
e.g.
<html:text property="abc" ... />
<c:out value="${abc}"/>
abc is always blank, even though I have value set by action sending to this page.
If you refer with ActionForm, you can retrieve the value. "abc" is one of the property of ActionForm(you created) which will be available in request scope as it has been set in ActionServlet during processing request.
So get the value like this, suppose your ActionForm is 'TestForm', then retrieve as ${TestForm.abc} or <c:out value="${TestForm.abc}"/>