• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Struts and JSP problem :(

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to JSP and Struts development. I have a display only form that
I have mapped to an ActionForm and an ActionClass. The form is displayed
OK, but I don't get any data in it. It is just a standalone form and am
trying to set the values for the form fields using Action class.
I am posting code snippets here:
*******ActionForm*************
public class page14ActionForm extends ActionForm
{

private String username;
private String name;
private String location;
public void setUsername(String username)
{
this.username = username;
}
public void setName(String name)
{
this.name = name;
}
...........
}
*******Action Class**********
public class page14Action extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{

page14ActionForm aForm = (page14ActionForm)form;

String uname="John" ;
request.setAttribute("username",uname);

return(mapping.findForward("success")) ;
}
}
**** JSP PAGE (Posting only for one field) ******
<html:form action="page14" name="page14Form" type="page14ActionForm" >
<table id="pg14table1" align="center" width="343" cellpadding="2" cellspacing="2">
<tr align="center">
<td ><bean:message key="app.username" /></td>
<td>
<html:text property="username" readonly="true" />

<bean efine id="username" name="page14Form" property="username" />
</td>
</tr>
******* STRUTS CONFIG************
<form-beans>
<form-bean name="page14Form" type="page14ActionForm" />
</form-beans>
<action-mappings>
<action path="/page14" type="page14Action"
name="page14Form" input="/common/page14.jsp" scope="request">

<forward name="success" path="/common/header.jsp" />
<forward name="failure" path="/common/footer.jsp" />
</action>
</action-mappings>
What am I missing here? I get a message cannot set null value. So looks like username is null, but why ?? I am setting it in ActionClass .
Any help is greatly appreciated.
Thanks in advance for all the responses.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With this code:
String uname="John" ;
request.setAttribute("username",uname);
In your Action you're setting a request attribute of "username" to "John". What you really want to be doing is setting a property of your ActionForm so that
<html:text property="username" readonly="true" />
returns with "John".
Try this in your Action instead:
aForm.setUsername("John");
Hope this helps.
Matt.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic