Java MVC to validate control data

2

I am new to MVC and would like to know where it is validated that the controls of the view have information. For example, to add two numbers in the following example, where is it valid that txtNumero1 and txtNumero2 of a view are not empty?

public class Modelo {
09     //Variables
10     private int valor1;
11     private int valor2;
12    
13 
14     public Modelo(){}
15 
16     public void set_valor1(int val){
17         this.valor1=val;
18     }
19 
20     public int get_valor1(){
21         return this.valor1;
22     }
23 
24     public void set_valor2(int val){
25         this.valor2=val;
26     }
27 
28     public int get_valor2(){
29         return this.valor2;
30     }
31 
32     public int sumar(){
33         return  this.valor1 + this.valor2;
34        
35     }
36   }

Vista

It has a text box ( txtNumero1 ), a text box ( txtNumero2 ), a text box ( txtResultado ) and a button btnSumar .

Driver

public class controlador implements ActionListener{
12 
13     private vista vista;
14     private Modelo modelo;
15 
16     
18     public controlador( vista vista , modelo modelo){
19         this.vista = vista;
20         this.modelo = modelo;
21         this.vista.cmdsumar.addActionListener(this);
22     }
23 
24     
25     public void iniciar_vista(){
26         vista.setTitle( "Sumar" );
27         vista.setLocationRelativeTo(null);
28     }
32 
33     
36     public void actionPerformed(ActionEvent e) {
37         modelo.set_valor1(Integer.valueOf( vista.txtNumero1.getText() ) );
38         modelo.set_valor2( Integer.valueOf( vista.txtNumero2.getText() )          vista.txtResultado.setText(String.valueOf(modelo.sumar()) );
41     }
42 
43 }

Where could I validate? From the model, from the controller or from both?

    
asked by Daniel 08.06.2016 в 22:47
source

3 answers

1

According to me the most correct thing would be to validate in the controller since it is who is responsible for processing the data so to speak the view only represents them and the model is the template of the objects that you are going to use so the strong part to program or the most robust usually is the controller and it would be more convenient to validate there

    
answered by 08.06.2016 в 23:15
0

If you are going to use validations of this type I would take advantage of the annotations JSR-349 ( link ). For example, they are not empty:

public class Modelo {
09     //Variables
       @NotEmpty
10     private int valor1;
       @NotEmpty
11     private int valor2;
12    
13 
14     public Modelo(){}
15 
16     public void set_valor1(int val){
17         this.valor1=val;
18     }
19 
20     public int get_valor1(){
21         return this.valor1;
22     }
23 
24     public void set_valor2(int val){
25         this.valor2=val;
26     }
27 
28     public int get_valor2(){
29         return this.valor2;
30     }
31 
32     public int sumar(){
33         return  this.valor1 + this.valor2;
34        
35     }
36   }

This way you would validate the complete bean, I also see that you use struts, personally I prefer Spring MVC, for me it is much more powerful. Here is an example of how it would be used:

@Controller
public class Controller {

    @RequestMapping(value = "/sumar", method = RequestMethod.POST)
    public String sumar(@Valid Modelo modelo, BindingResult result) {
        // lógica
    }
}

With the @Valid annotation, it automatically performs the validation of the input data. This way you do not have to build specific methods for all validations.

I hope I helped you something!

    
answered by 09.06.2016 в 08:14
0

Usually when we talk about MVC we refer to a web application. If you are starting with this paradigm, I recommend you to learn Spring MVC with videos from Kevin Bowersox's InfiniteSkills, they are in English, but it is the best.

When you mean validation, you understand two things:

  • Validation of the Object (You can validate in the controller using Spring)
  • Field validation via JSP (You can use HibernateAnnotations or JS)
  • The combination of both makes your application more robust.

    Cristian gives you a taste of how powerful Spring can be.

    Greetings

        
    answered by 10.06.2016 в 21:33