0

I'm thinking about good approaches to develop web software. Spring is great for JDBC stuff but it also has a nice feature for autocompleting faulty forms, nice backing beans for prefilling forms and so on.

Now I started with JSF and it seems more correct than Spring for doing MVC. I like the xhtml approaches, including templates, defining these webflows in the faces-config.xml and so on.

But there is no clear separation of concerns because both frameworks can work with forms, and I am pretty sure you will have to make your mind up if Faces or Spring shall make the forms. I'm tending towards JSF but I'm missing these nifty error handling and prefilling features.

Can JSF do these things also? I'm new to JSF so I'm not sure how mighty it is.

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149

1 Answers1

2

but I'm missing these nifty error handling and prefilling features. Can JSF do these things also? I'm new to JSF so I'm not sure how mighty it is.

JSF has builtin validation, e.g. <h:inputText required="true">, <f:validateLongRange>, <f:validateRegex> and also conversion, e.g. <f:convertDateTime>, <f:convertNumber>, etc (see them all here). JSF also supports JSR 303 Bean Validation which is controlled by annotations like @NotNull, @Min, @Max, @Pattern, etc on model objects. All validation/conversion errors end up in a <h:message> bound to the input element or a global <h:messages>. This JSF 2.0 tutorial handles validation in depth.

As to prefilling, just set the desired model in bean's (post) constructor or by <f:viewParam> if some ID is to be obtained as request parameter. Basically:

public class Bean {

    private Entity entity;

    @EJB
    private EntityService entityService;

    @PostConstruct
    public void init() {
        entity = entityService.find(someId);
    }

    // ...
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So there is no need anymore for Spring in MVC? :-) – Franz Kafka Sep 03 '11 at 22:21
  • Java EE 6 comes along with EJB 3.1, CDI and Bean Validation which all can supplant Spring. Spring grew during J2EE ages when EJB2 was terrible and dependency injection and validation weren't provided by J2EE. Sun/Oracle learnt from it and added similar APIs for Java EE 5 and 6 so that containers just support it out the box. See also http://stackoverflow.com/questions/2499323/jee6-vs-spring-3-stack – BalusC Sep 03 '11 at 22:24