Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello everyone, Doubts concerning html:form

Status
Not open for further replies.

SuperMoonster

Programmer
Aug 11, 2005
59
BR
Hello everyone,

I'm new to struts and I have some doubts concerning the html:form tag.

1. I have a form that calls an action... I want to pass some parameters to this action. How do I do it through my form? Is there a tag like "paramId" or something? Or should I do something like <html:form action="/Action.do?param=value">??

2. I have some data in my database I wanna show to my user. I want to put it in html:text so my user can edit the values. So, I need a form to do it, right? How do I do to show them, when I know that my parameters are in my request scope?

I looked for this in the web and couldn't find an answer. Thanks you all very much.
 
Hi

1. Yes you have to do <html:form action="/Action.do?param=value"> as there is no paramId from html:form. Make sure you have the params defined in the ActionForm.

2. If you have the values in the request you need to copy those values in the ActionFrom and then forward the request to the .jsp page.

Best would be using EL tags.

Cheers
Venu
 
1. How do I define the params in my ActionForm? I know I can define them in my struts-config.xml, but that's not what you're talking about, is it?

2. I really didn't get it. Can you please explain it better?

Thanks for your kindness
 
Hi,

1. What I mean was your ActionFrom should have the properites defined but it is ok even if you do not deifne them in the struts-config.xml or even in ActionForm as you can read them in the Action as you have the request.
2. Here is the example how you can populate the actionFrom from your request

Ex:
Code:
<!-- From Bean -->
<form-bean name="tForm" type="org.apache.struts.validator.DynaValidatorForm">
       <form-property name="name" type="java.lang.String"/>
       <form-property name="age" type="java.lang.String"/>
    </form-bean>

<!-- actionMapping -->
 <action path="/setForm"
        type="com.test.action.DummyAction"
        name="tForm"
        scope="request"
        validate="false">
        <forward name="success" path="/tektips.jsp"/>
 </action>
 
<!-- Action Class -->
public class DummyAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @throws javax.servlet.ServletException
   * @throws java.io.IOException
   * @return 
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    DynaActionForm testForm = (DynaActionForm) form;
    // you can set the values from request, session into the form
    testForm.set("name","My Name");
    testForm.set("age","28");
    return mapping.findForward("success");
  }
}

<!-- JSP page -->
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Tek-Tips</title>
  </head>
  <body>
  <html:form action="setForm.do" scope="request">
   Name:<html:text property="name" />
   </br>
   Age:<html:text property="age" />
  </html:form>
  </body>
</html>

Exectue the setForm.do will pre-populate the jsp page

Hopet this will help you

Cheers
Venu
 
Venus, thank you very much, but what I want is the opposite.
I want to take a parameter from the JSP and use it in the action. And I don't wanna use scriplets.
I have a html:form and I wanna get the value of html:text and send it to my action
 
Let me tell you what I'm trying to do.

I tried passing the parameter two ways. The first was with html:hidden, like this:

Code:
  <html:form action="/ListaColecao.do" method="post" focus="descricao">  
      <table border="0" align="center">
         <tr><td width="50%">Coleção</td><td width="50%"><html:text property="descricao" /></td></tr>      
         <tr><td width="50%"> </td><td width="50%"> </td></tr>      
         <tr><td width="50%" align="center"><html:submit value="Buscar"/> </td><td width="50%" align="center"><html:reset value="Limpar"/></td></tr>      
      </table><br/>
      <html:hidden name="desc" property="descricao" value="this.descricao" />
   </html:form>

The second was through the URL, like this:

Code:
  <html:form action="/ListaColecao.do?desc=this.descricao" method="post" focus="descricao">  
      <table border="0" align="center">
         <tr><td width="50%">Coleção</td><td width="50%"><html:text property="descricao" /></td></tr>      
         <tr><td width="50%"> </td><td width="50%"> </td></tr>      
         <tr><td width="50%" align="center"><html:submit value="Buscar"/> </td><td width="50%" align="center"><html:reset value="Limpar"/></td></tr>      
      </table><br/>
   </html:form>

As you see, I was trying to get the value from the html:text field descricao and send it as a param named desc.

None of them work. In my action, the parameter "desc" keeps coming null. Am I setting it the wrong way?

Maybe I could be retrieving it the wrong way? I'm doing the following to retrieve:

Code:
                List lista = null;            
            
                if (request.getParameter("desc") == null ) {
                    lista = ColecaoService.getInstance().getColecaoList();
                }
                else {
                   String descricao = request.getParameter("desc").toString();                    
                   lista = ColecaoService.getInstance().getColecaoEspecificaList(descricao);                    
                }

and it always comes null :(

Something's wrong, I just don't know what. Any ideas?

Thank you very much.
 
Hi,

Try using javascript will solve the above problem.

Code:
 <script language="javascript">
   function appendTest(){
    document.forms[0].desc.value= document.forms[0].descricao.value;
   }
  </script>

<html:form action="/ListaColecao.do" method="post" focus="descricao" onsubmit="appendTest()">  
      <table border="0" align="center">
         <tr><td width="50%">Coleção</td><td width="50%"><html:text property="descricao" /></td></tr>      
         <tr><td width="50%"> </td><td width="50%"> </td></tr>      
         <tr><td width="50%" align="center"><html:submit value="Buscar"/> </td><td width="50%" align="center"><html:reset value="Limpar"/></td></tr>      
      </table><br/>
      <html:hidden property="desc" value="" />
   </html:form>

Cheers
Venu
 
I've tried javascript. My jsp is this one:

Code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>  
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Coleçôes</title>
                
        <link rel="stylesheet" type="text/css" href="../config/estilos.css" />
        
        <html:base/>

     </head>
     <body>

    <script language="javascript">
        function setaParametros(){
        document.forms[0].desc.value= document.forms[0].descricao.value;
      }
      
    </script>


    <span style="font-family:Sans-serif;">

    <table width="100%" align="center">
    <tr><td> Coleções </td></tr>
    <tr bgcolor="#000000"> <td></td></tr>
    </table>

    <br><br><br>

  <html:form action="/ListaColecao.do" method="post" focus="descricao" onsubmit="setaParametros()">  
      <table border="0" align="center">
         <tr><td width="50%">Coleção</td><td width="50%"><html:text property="descricao" /></td></tr>      
         <tr><td width="50%"> </td><td width="50%"> </td></tr>      
         <tr><td width="50%" align="center"><html:submit value="Buscar"/> </td><td width="50%" align="center"><html:reset value="Limpar"/></td></tr>      
      </table><br/>
      <html:hidden property="descricao"/>
   </html:form>   
   
   
<br>
<html:errors/>      
    
    </span>
    </body>
</html>

Still, doesn't work. I've tried creating a function to display a pop-up window just to see if it is calling the javascript function, and it is. But this function above doesn't work.

I'm thinking maybe it's because it's not recognizing who is form[0]? Or maybe it's just not being able to send the param?

Thanks again for the help
 
Hi,

The hidden field property needs to be desc not descricao.


Cheers
Venu

 
Not possible.

If I change it's property tag from "descricao" to "desc", it gives me an error saying that there's no getter method for desc.
Then, what I did was to change it's name to "desc" and it's property to "descricao". A new error comes, saying that cannot find bean descricao in any scope. I can't leave it without the property tag, because it says it's mandatory.

Is javascript the only way to go here?
 
Geez, I was being kinda stupid. There's no need to send the param. It's a form, so when it's submitted every field goes within. So, all I had to do was a request.getParameter in my Action. No need of hidden fields or javascript.

Thank you very much for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top