You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@roller.apache.org by Dudee <gu...@gmail.com> on 2007/10/01 13:15:20 UTC

Get values from form to struts action

I have created my own form on a velocity page and I have written a struts
action but I don't know how to get the values from the form into the struts
action. I have also created a action bean with the same name as the the
action file.

So, how should I do to get the values from the form in to the struts action
file?
And what method in the struts action is executed when the action is called.
As it is now, only my constructor is called so I have added a call there to
the save method. 

Is it possible to use something like "request.getAttribute(name)" for
example to get the value of the name from the form?


-- 
View this message in context: http://www.nabble.com/Get-values-from-form-to-struts-action-tf4547634s12275.html#a12977174
Sent from the Roller - Dev mailing list archive at Nabble.com.


Re: Get values from form to struts action

Posted by Dudee <gu...@gmail.com>.
OK, but do you know why the save method isn't called directly as in for
example the CreateUser action?


Dave Johnson-8 wrote:
> 
> I don't see anything obviously wrong.
> 
>  - Dave
> 
> 
> 
> On 10/1/07, Dudee <gu...@gmail.com> wrote:
>>
>> Ok. But can you please tell me whats wrong with the following code.
>>
>> Struts.xml:
>> <action name="contact!*" method="{1}"
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Get-values-from-form-to-struts-action-tf4547634s12275.html#a12979440
Sent from the Roller - Dev mailing list archive at Nabble.com.


Re: Get values from form to struts action

Posted by Dudee <gu...@gmail.com>.
The problem is that bean doesn't get pupulated.
Where should that happen? In the action class, I create a new Bean so how
should that bean get the values from the form? I must be missing something?
I've been over it 100 times now but can't figure it out. Help please!!




Dave Johnson-8 wrote:
> 
> I don't see anything obviously wrong.
> 
>  - Dave
> 
> 
> 
> On 10/1/07, Dudee <gu...@gmail.com> wrote:
>>
>> Ok. But can you please tell me whats wrong with the following code.
>>
>> Struts.xml:
>> <action name="contact!*" method="{1}"
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Get-values-from-form-to-struts-action-tf4547634s12275.html#a12981538
Sent from the Roller - Dev mailing list archive at Nabble.com.


Re: Get values from form to struts action

Posted by Dave <sn...@gmail.com>.
I don't see anything obviously wrong.

 - Dave



On 10/1/07, Dudee <gu...@gmail.com> wrote:
>
> Ok. But can you please tell me whats wrong with the following code.
>
> Struts.xml:
> <action name="contact!*" method="{1}"
>
>

Re: Get values from form to struts action

Posted by Dudee <gu...@gmail.com>.
Ok. But can you please tell me whats wrong with the following code. 

Struts.xml:
<action name="contact!*" method="{1}"
               
class="org.apache.roller.weblogger.ui.struts2.contact.Contact">
            <result name="input" type="tiles">.Contact</result>
  </action>


velocity-page:
<form id="contactForm" name="contactForm" onsubmit="return true;"
action="/roller-weblogger/roller-ui/contact/contact!save.rol" method="post">
    <table class="formtable">
        <tbody>
			<tr>
	            <td class="label"><label for="username">Username</label></td>
	            <td class="field"><input name="bean.user" size="30"
maxlength="48" value="" id="contact_bean_user" type="text">
				</td>
	            <td class="description">Username</td>
	        </tr>
	        
	        <tr>
	            <td class="label"><label for="blog">Blog</label></td>
	            <td class="field"><input name="bean.blog" size="30"
maxlength="48" value="" id="contact_bean_blog" type="text"></td>
	            <td class="description">Blog</td>

	        </tr>       	              	        
	    </tbody></table>   
	    <br>
	    <br>

    <div class="control">
		<input type="submit" id="contact_save" name="save" value="Save"/>
    </div>
</form>


Contact.java

public class Contact extends UIAction {
    
    private static Log log = LogFactory.getLog(Contact.class);
    
    // a bean to store our form data
      private ContactBean bean = new ContactBean();
    
    public Contact() {
        this.actionName = "Contact";
        this.pageTitle = "contact.title.contact";
		log.info("Testing 1");
    }
       

    public String execute() {
        log.info("Execute contact");
        return INPUT;
    }
    
    
    /**
     * Save form info
     */
    public String save() {

		ContactBean cb = getBean();
		String user = cb.getUser();
		String blog = cb.getBlog();
		log.info(user + " "+blog);
		log.info("Testing 2");
		return INPUT;
    }

    public ContactBean getBean() {
        return bean;
    }

    public void setBean(ContactBean bean) {
        this.bean = bean;
    }	    
}


ContactBean.java
public class ContactBean {
    
    private String blog = null;
    private String user = null;

    public String getUser() {
        return user
    }

    public void setUser(String user) {
        this.user = user;
    }
    
    public String getBlog() {
        return blog
    }

    public void setBlog(String blog) {
        this.blog = blog;
    }   
}


I only get "Testing 1" in the log and if I include "save()" in the end of 
"public Contact()" I get "StandardWrapperValve:invoke - Servlet.service()
for servlet default threw exception java.lang.NullPointerException at
org.apache.roller.weblogger.ui.struts2.contact.ContactBean.getUser".  Would
be really appreciated If you could tell me if I have made some obvious
error.







Dave Johnson-8 wrote:
> 
> On 10/1/07, Dudee <gu...@gmail.com> wrote:
>> I have created my own form on a velocity page and I have written a struts
>> action but I don't know how to get the values from the form into the
>> struts
>> action. I have also created a action bean with the same name as the the
>> action file.
>>
>> So, how should I do to get the values from the form in to the struts
>> action
>> file? And what method in the struts action is executed when the action is
>> called.
>> As it is now, only my constructor is called so I have added a call there
>> to
>> the save method.
>>
>> Is it possible to use something like "request.getAttribute(name)" for
>> example to get the value of the name from the form?
> 
> We don't do that anywhere in Roller, but it's not a bad idea. Struts 2
> will map request parameters to fields on your action. You'll have to
> read up on Struts 2 to figure out how to do it.
> 
> - Dave
> 
> 

-- 
View this message in context: http://www.nabble.com/Get-values-from-form-to-struts-action-tf4547634s12275.html#a12978951
Sent from the Roller - Dev mailing list archive at Nabble.com.


Re: Get values from form to struts action

Posted by Dave <sn...@gmail.com>.
On 10/1/07, Dudee <gu...@gmail.com> wrote:
> I have created my own form on a velocity page and I have written a struts
> action but I don't know how to get the values from the form into the struts
> action. I have also created a action bean with the same name as the the
> action file.
>
> So, how should I do to get the values from the form in to the struts action
> file? And what method in the struts action is executed when the action is called.
> As it is now, only my constructor is called so I have added a call there to
> the save method.
>
> Is it possible to use something like "request.getAttribute(name)" for
> example to get the value of the name from the form?

We don't do that anywhere in Roller, but it's not a bad idea. Struts 2
will map request parameters to fields on your action. You'll have to
read up on Struts 2 to figure out how to do it.

- Dave