File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to display a particular value in <s:textfield> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to display a particular value in <s:textfield>" Watch "How to display a particular value in <s:textfield>" New topic
Author

How to display a particular value in <s:textfield>

piyush sah
Greenhorn

Joined: Apr 11, 2011
Posts: 12
Hi friends,
I'm using struts 2.0 . I'm able to connect to the database & retrieve the values of the database in my textfield by using getModel fundamental. My action file for that purpose is as follows :

/**
*
*/
package com.timscdr.project.action;
import java.util.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.timscdr.project.Business.IntfStudentBO;
import com.timscdr.project.BusinessImpl.StudentBO;
import com.timscdr.project.model.StudentVO;
/**
* @author User
*
*/
public class StudentAction extends ActionSupport implements ModelDriven<StudentVO> {

/**
*
*/List<StudentVO> result=new ArrayList<StudentVO>();



public List<StudentVO> getResult() {
return result;
}

public void setResult(List<StudentVO> result) {
this.result = result;
}

private static final long serialVersionUID = 1L;

private StudentVO student1 = new StudentVO();
public StudentVO getStudent1() {
return student1;
}

public void setStudent1(StudentVO student1) {
this.student1 = student1;
}

@Override
public StudentVO getModel() {
// TODO Auto-generated method stub
return student1;
}

/*
* calling BO from here action will have only following functionality
* 1. Call the correct BO methods
* 2. Redirect to correct JSP page after checking the result from them BO
*/
public String addStudent(){
String result;
IntfStudentBO studentObj = new StudentBO();
result = studentObj.addStudent(student1);

if(result == "0"){
return SUCCESS;
}
else{
return ERROR;
}
}



public String roleSelect(){
IntfStudentBO studentObj = new StudentBO();
result = studentObj.SelectStudent(student1);
return SUCCESS;

} // End of roleSelect
public String selectStudent()
{

IntfStudentBO studentObj = new StudentBO();
result = studentObj.SelectStudent(student1);
return SUCCESS;

}
}
......This is working absolutely fine.

***************************************************************************************************************
2. Now my question is I have a function to autoGenerate String primary key (i.e it automatically incearments my primary key eg : STU001,STU002.........etc..)
the function is as follows :

public String autoGen(IDVO id) {
// TODO Auto-generated method stub
String r1;
IntfIdDAO idDao= new IDDAO();
r1 = idDao.autoGen(id);
r1=r1.substring(3);
int x = Integer.parseInt(r1);
x = x+1;
String zero = "000";
zero = zero + String.valueOf(x);
zero=(new StringBuffer(zero).reverse()).toString();
zero = zero.substring(0,3);
zero = (new StringBuffer(zero).reverse()).toString();
r1="STU"+zero;
System.out.println(r1);

return r1;

}



Now even the function is running fine.But how to I set the value returned by the above mentioned funtion in the <s:textfield> and display it on the jsp

What I want is :
Suppose I have a link say addstudent on clicking that link I must be redirected to a jsp page to addstudent(There's no problem in this). But how to I make the primary key field in the jsp page to the auto generated key returned by my function......In other words is there anything in struts2.0 as simple as a
textfield.setText("sometext") as we used to do in swings.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
hey piyush i have tried it 2 days back only its not possible to display any default value in textfield in struts ike other pages

but you can try this

<%=r1 %>
but if you will right

<s:textfiled property="abc" value="<%=r1 %>"/>

try it just printing the value as
<%=r1 %>


if you find any other way then please also let me know about it

thank you

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Aditi / Piyush - It can be easily done using OGNL.

Your textfield should be mapped to some property in your action class, lets say your property name is

String textVal;

and it has its correspoding getters and setters, you can retain the value in your jsp without using scriplets ( highly discouraged when you are using a framework like struts2 these days).



Edit: fixed a typo.


Time is what we want the most, but what we use the worst. -- William Penn
piyush sah
Greenhorn

Joined: Apr 11, 2011
Posts: 12
Thanks Robin for your wizardry it worked . You saved my day !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display a particular value in <s:textfield>
 
Similar Threads
Struts2 problem with ModelDriven interceptor
Struts 2 validation using Annotation is not working
validation by annotations
execute method of ActionSupport Class
Retrieving database values inside the <s:select> tag