I want to pass a session value as a hidden form value to an action class.
I have seen several examples but nothing worked for me; I get a null in the action.
Update.jsp:
<s:hidden name="name" value="%{#session.sname}" />
<s:property value="#session.sname"></s:property>//works fine and printing username
User name is stored in session. I want to send this name to action class but I am unable to send.
UpdateAction.java:
public class UpdateAction extends ActionSupport {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public string execute() {
System.out.println("Username"+name);//getting null value
}
}
You should try:
<s:hidden value="%{#session['sname']}" name="aName"></s:hidden>
Related
I have a ui tag select
<s:select list="studentList" name="selectedstudent" listKey="studentID"
listValue="studentName" headerKey="0"
headerValue="--All Student--" theme="simple"></s:select>
in java code i have selectedstudent returns let say 4 how can I get the name of student in java.
You can get the name by id in selectedstudent. For this purpose if you don't have a students list in your action, you should load them from database. Otherwise searching a Student object by id would return a name if you provide a getter for the object. Keep in mind that you have already specified a listValue as studentName. This value should be taken from a Student and thus a list should be List<Student> as well.
If your studentList is in session you can retrieve student name as follow (your code may be different):
List<Student> studentList = (List<Student>)ActionContext.getContext().getSession().get("studentList");
//You should ovveride equals(and hashCode) to point the Student equality to studentID
//You should also check if studentList.indexOf(selectedstudent) != -1
Student student = studentList.get(studentList.indexOf(selectedstudent));
String studentName = student.getStudentName();
Business layer
Student.java (model)
public class Student {
//Getter and Setter Method.
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
}
struts.xml
<action name="list_action" class="controller.ListAction">
<result name="success">struts_select.jsp</result>
</action>
package controller;
ListAction.java
private List<Student> student_list = new ArrayList<Student>();//Getter and Setter
#Override
public String execute() throws Exception {
//do your stuff
//list collect from database.
student_list=(List<Student>) criteria.list();
}
struts_select.jsp
<s:select label="Student List"
listKey="id" //use your Student.java member variable of id signature
listValue="name" //use your Student.java member variable of name signature
headerKey="0" headerValue="Select student Name"
list="s"
name="student_info" />
Hey guys I'm having some problems with a project that I'm doing :/
I have this code
public class ListaAlumnoAction {
private List<Alumno> alumnos;
public String execute(){
String camino="success";
EntityManager em= Utilitario.getInstance().getEntityManager();
Query query=em.createQuery("Select o From Alumno o");
alumnos=query.getResultList();
return camino;
}
public List<Alumno> getAlumnos() {
return alumnos;
}
public void setAlumnos(List<Alumno> alumnos) {
this.alumnos = alumnos;
}
}
This method gives me a list of students. But in this case I just want 1 student, searching by code for example or by name and last name. The thing is I don't know what kind of tools use for this purpose.
Thanks in advanced.
For searching the student by specified input criteria, you are in need of passing the required input from the page to the action class. And need to modify the query which will search the student record by mentioned name or last name or desired input.
For example, assume student name is the input criteria
To pass the student name from the page:
<s:form>
......
<s:textfield name="studentName" label="Student Name"></s:textfield>
<s:submit value="Search" name="submit" action="ListaAlumnoAction"/>
......
</s:form>
Here the studentName will be the input for search operation and ListaAlumnoAction is action which will fired when Search button clicked.
To get the Student Name in the Action Class:
public class ListaAlumnoAction {
private List<Alumno> alumnos;
/* Please note both the text filed name in the page and attribute should be same "studentName" */
private String studentName;
public String execute(){
String camino="success";
EntityManager em= Utilitario.getInstance().getEntityManager();
// "name" variable will hold the studentName
String name = getStudentName();
// Modify the query, by passing the name if it is not null
Query query=em.createQuery("Select o From Alumno o");
alumnos=query.getResultList();
return camino;
}
public List<Alumno> getAlumnos() {
return alumnos;
}
public void setAlumnos(List<Alumno> alumnos) {
this.alumnos = alumnos;
}
// getter and setter for the studentName
public String getStudentName(){
return studentName;
}
public void setStudentName(String studentName){
this.studentName = studentName;
}
}
I use Struts 2 Framework and i want to pass a parameter in my action like this localhost:8080/MyApp/ModifierMessage.action?id=9. In my jsp page i have the following text with the action:
Modifier
Someone could help me to add dynamic id to my action ?
Use <s:url> and <s:a> tags. For example is your dynamic id is called dynamic_id:
<s:url var="myUrl" action="ModifierMessage.action" namespace="/OCC">
<s:param name="id">%{dynamic_id}</s:param>
</s:url>
<%-- The link --%>
<s:a href=%{#myUrl}>Modifier</s:a>
public class MyAction extends ActionSupport {
private int id;
public String execute() {
...
this.id = 123;
return SUCCESS;
}
public int getId() { return this.id; }
public void setId(int id) { this.id = id; }
...
}
In the above code if it returns SUCCESS then the browser will be forwarded to
/<app-prefix>/myNamespace/otherAction.action?id=123
I have uncovered some strange behavior trying to set a value with a view parameter. The code below does NOT work...upon submission of the form with the view parameter, the id value is NOT set. However, if the comments are removed from the getId and setId methods, the id value IS properly set. Is there some issue with JSF EL when accessing generics, or am I doing something terribly wrong? Can anyone explain this behavior?
Interestingly, I receive no exceptions...the value is simply not set. In fact, the setId method in the abstract class is not even called.
public abstract class AbstractBusinessObjectAction<E, ID extends Serializable> {
protected abstract BusinessObjectManager<E, ID> getBusinessObjectManager();
public ID getId() {
ID id = getBusinessObjectManager().getId();
return id;
}
public void setId(final ID id) {
getBusinessObjectManager().setId(id);
}
}
#Named("configuration")
#ConversationScoped
public class ConfigurationAction extends AbstractBusinessObjectAction<ConfigurationParameter, Long> {
#Inject
private ConfigurationParameterManager manager;
protected BusinessObjectManager<ConfigurationParameter, Long> getBusinessObjectManager() {
return manager;
}
// public Long getId() {
// Long id = super.getId();
// return id;
// }
// public void setId(Long id) {
// super.setId(id);
// }
}
Following is an except from the .xhtml:
<f:metadata>
<f:viewParam id="entityIdParam" name="entityId" value="#{configuration.id}" />
</f:metadata>
Adding a converter solved the problem.
<f:viewParam id="entityIdParam" name="entityId" value="#{configuration.id}" >
<f:converter converterId="javax.faces.Long" />
</f:viewParam>
To the best of my knowledge, this is the reason:
Because the id property is of type ID extends Serializable in the abstract class, JSF tries to install a non-existant Serializable converter. The reason the code works when I uncomment the methods indicated, is that JSF properly detects that it needs a javax.faces.Long converter based on the explicit declaration of Long on the property methods for the "id" property.
Thanks to #Damian for the tip!
I am facing a problem in Struts2, In my sample application I have Array of objects (Say person name) , I need to display these names as a editable text fields , I am using Iterators for this , I am successful in diaplying, But When I cange the same value and submit the form. I am getting the entire array which holds null value.
Say for Exaple in my form bean I have a property
Name [] names;
In my JSP I have the iterator as
If there are 3 names then I can get these names on the UI if I can initialized them with some dummy value but when I edit and sumbit, then "names" array is not getting updated. Please help me in this regard
In Struts2, when you need to repopulate the list of bean items, you need to refer them through indices. Please refer the example below:
Bean class:
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Action class:
public class PersonAction extends ActionSupport {
private List<Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
//Initial Load method
#Override
public String execute() {
persons = new ArrayList<Person>();
int alpha = 65;
for(int i = 0; i < 3 ; i++) {
Person person = new Person();
person.setId(i);
person.setName(String.valueOf((char)alpha++));
persons.add(person);
}
return SUCCESS;
}
//Function that handles the form submit
public String updatePerson() {
for(Person person : persons) {
System.out.println(person.getId() + ":" + person.getName());
}
return SUCCESS;
}
}
Page:
<s:form action="doUpdate">
<s:iterator value="persons" status="stat" var="person">
<s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/><br/>
</s:iterator>
<s:submit value="Submit"/>
</s:form>
When you submit the above form, the url would look like doUpdate?persons[0].name=A1&persons[1].name=B1&persons[2].name=C1. Similarly if you need to update id of the first person object, you will append persons[0].id=3 to the url using form. In the <s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/>, you tell that the predefined value is person's name, for each object. The name attribute is to set the rendered html input element; the name which will be referenced in the url when the form is submitted. You will get a clear idea if you look into the generated html.
Use the iterator tag of struts2, if list is not empty then use :
<s:iterator value="names" status="namesStatus">
<s:property/><br/>
</s:iterator>