You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Sokab <So...@gmail.com> on 2017/03/28 15:49:38 UTC

How save entity after clicking on submit button inside Form Wicket class.

Hi Everyone. When I try save entity inside simple WebPage like this
everything is ok:

import com.example.entities.Person;
import javax.ejb.EJB;

public class Register extends WebPage{
    @EJB
    NewSessionBeanLocal beanLocal;
    
    public Register(){
      
        NavomaticBorder navomaticBorder = new
NavomaticBorder("navigationBorder");      
        RegisterForm form = new RegisterForm("form");  
        navomaticBorder.add(form);
        add(navomaticBorder);
        
      // save entity to database for test
        Person person = new Person();
        person.setSurname("ABC");
        person.setUsername("DEF");

        beanLocal.addPerson(person);      // IT IS OK
    }
}

//************************************************************************************************************************

Html Form:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,
initial-scale=1.0">
    </head>
    <body>
        
            <form wicket:id="form">
                Username <input type="text" wicket:id="username"/>
                Suername <input type="text" wicket:id="surname"/>
                <input value="Save" name="save" type="submit" />
            </form>      
        
    </body>
</html>

//************************************************************************************************************************

but when i try do this inside Form, becouse I want save entity after click
on Subimt button (register Person):

import com.example.Repository.NewSessionBeanLocal;
import com.example.entities.Person;
import javax.ejb.EJB;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;

public class RegisterForm extends Form{    
    @EJB
    NewSessionBeanLocal beanLocal;
    
    Person person = new Person();
    
    public RegisterForm(String id) {
        super(id);        
        CompoundPropertyModel model = new CompoundPropertyModel(person);
        this.setModel(model);
        
        this.add(new TextField("username"));
        this.add(new TextField("surname"));
    }
    
    @Override
    public void onSubmit() {
        Person p = (Person)this.getModelObject();
        System.out.println("TO: : : "+ p.getUsername()+ " "+
p.getSurname());  // shows right data
        
      //Person p = new Person();
      //p.setSurname("ABC");
     //p.setUsername("DEF");

        beanLocal.addPerson(p); // <-- PROBLEM IS HERE
    }    
    
}


 I have error:
Unexpected RuntimeException:

Last cause: null
WicketMessage: Method onFormSubmitted of interface
org.apache.wicket.markup.html.form.IFormSubmitListener targeted at
[RegisterForm [Component id = form]] on component [RegisterForm [Component
id = form]] threw an exception

Root cause:
java.lang.NullPointerException
     at
com.example.navigationBefore.RegisterForm.onSubmit(RegisterForm.java:34)
(...)

Something I'm doing wrong? I Should not save entity inside onSubmit() Form
method?  Thank you for any advice.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-save-entity-after-clicking-on-submit-button-inside-Form-Wicket-class-tp4677474.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How save entity after clicking on submit button inside Form Wicket class.

Posted by Sokab <So...@gmail.com>.
Thanks for the advice! :)

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-save-entity-after-clicking-on-submit-button-inside-Form-Wicket-class-tp4677474p4677656.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How save entity after clicking on submit button inside Form Wicket class.

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

I wonder why it worked with a WebPage but broke when used in a Form ?
I think it should not work in a Page as well.

From your earlier posts I've got the impression that you use wicket-cdi.
This integration supports @Inject.
If you need to use @EJB then you need to use
https://github.com/wicketstuff/core/wiki/Java-EE-Inject

On Wed, Mar 29, 2017 at 10:14 PM, Sokab <So...@gmail.com> wrote:

> Hi Sebastian. Thank You For answer.
>
> I solved this problem after I changed inside Form:
> @EJB
> NewSessionBeanLocal beanLocal
>
> to
>
> @Inject
> NewSessionBeanLocal beanLocal       ..and it works.
>
> I didn't configured the dependency injection because I don't know how if
> You
> ask me about some separate file or class (Sorry I start to learn Wicket). I
> just tried to change atributes inside @EJB( ... like: beanName, lookup ...)
> but that did not help.
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.
> n4.nabble.com/How-save-entity-after-clicking-on-submit-
> button-inside-Form-Wicket-class-tp4677474p4677512.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: How save entity after clicking on submit button inside Form Wicket class.

Posted by Sokab <So...@gmail.com>.
Hi Sebastian. Thank You For answer.

I solved this problem after I changed inside Form:
@EJB 
NewSessionBeanLocal beanLocal 

to

@Inject
NewSessionBeanLocal beanLocal       ..and it works. 

I didn't configured the dependency injection because I don't know how if You
ask me about some separate file or class (Sorry I start to learn Wicket). I
just tried to change atributes inside @EJB( ... like: beanName, lookup ...)
but that did not help.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-save-entity-after-clicking-on-submit-button-inside-Form-Wicket-class-tp4677474p4677512.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How save entity after clicking on submit button inside Form Wicket class.

Posted by Bas Gooren <ba...@iswd.nl>.
Hi Sokab,


I’m not an expert regarding EJB injection, but my first guess is that your


@EJB

NewSessionBeanLocal beanLocal


is null inside your form. Can you check this please?


If that is the case, it’s probably related to the way you have set up
injection. Can you share how you have configured the dependency injection?

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 28 maart 2017 bij 18:59:33, Sokab (sokab3773@gmail.com) schreef:

Hi Everyone. When I try save entity inside simple WebPage like this
everything is ok:

import com.example.entities.Person;
import javax.ejb.EJB;

public class Register extends WebPage{
@EJB
NewSessionBeanLocal beanLocal;
br/> public Register(){{
br/> NavomaticBorder navomaticBorder = new
NavomaticBorder("navigationBorder"); br/> ReggisterForm form = new
RegisterForm("form"); br/> naavomaticBorder.add(form);
add(navomaticBorder);
br/> // save entity to database for test <
Person person = new Person();
person.setSurname("ABC");
person.setUsername("DEF");

beanLocal.addPerson(person); // IT IS OK
}
}

//************************************************************************************************************************


Html Form:

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
</head>
<body>
br/> <form wicket:id="form">
Username <input type="text" wicket:id="username"/>
Suername <input type="text" wicket:id="surname"/>
<input value="Save" name="save" type="submit" />
</form> br/> br/> </body> <
</html>

//************************************************************************************************************************


but when i try do this inside Form, becouse I want save entity after click
on Subimt button (register Person):

import com.example.Repository.NewSessionBeanLocal;
import com.example.entities.Person;
import javax.ejb.EJB;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;

public class RegisterForm extends Form{ br/> @@EJB
NewSessionBeanLocal beanLocal;
br/> Person person = new Person();
br/> public RegisterFForm(String id) {
super(id); br/> CompoundPropertyModel mmodel = new
CompoundPropertyModel(person);
this.setModel(model);
br/> this.add(new TextFField("username"));
this.add(new TextField("surname"));
}
br/> @@Override
public void onSubmit() {
Person p = (Person)this.getModelObject();
System.out.println("TO: : : "+ p.getUsername()+ " "+
p.getSurname()); // shows right data
br/> //Person p = new Person();
//p.setSurname("ABC");
//p.setUsername("DEF");

beanLocal.addPerson(p); // <-- PROBLEM IS HERE
} br/> br/>}}


I have error:
Unexpected RuntimeException:

Last cause: null
WicketMessage: Method onFormSubmitted of interface
org.apache.wicket.markup.html.form.IFormSubmitListener targeted at
[RegisterForm [Component id = form]] on component [RegisterForm [Component
id = form]] threw an exception

Root cause:
java.lang.NullPointerException
at
com.example.navigationBefore.RegisterForm.onSubmit(RegisterForm.java:34)
(...)

Something I'm doing wrong? I Should not save entity inside onSubmit() Form
method? Thank you for any advice.

-- 
View this message in context:
http://apache-wicket.1842946.n4.nabble.com/How-save-entity-after-clicking-on-submit-button-inside-Form-Wicket-class-tp4677474.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org