File tree Expand file tree Collapse file tree
java/com/baeldung/springbootmvc/jsfapplication Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919 <groupId >org.springframework.boot</groupId >
2020 <artifactId >spring-boot-starter-web</artifactId >
2121 </dependency >
22+ <!-- JSF-->
23+ <dependency >
24+ <groupId >com.sun.faces</groupId >
25+ <artifactId >jsf-api</artifactId >
26+ <version >2.2.9</version >
27+ </dependency >
28+ <dependency >
29+ <groupId >org.apache.tomcat.embed</groupId >
30+ <artifactId >tomcat-embed-jasper</artifactId >
31+ </dependency >
32+ <dependency >
33+ <groupId >javax.faces</groupId >
34+ <artifactId >javax.faces-api</artifactId >
35+ <version >2.1</version >
36+ </dependency >
37+ <dependency >
38+ <groupId >javax.servlet</groupId >
39+ <artifactId >jstl</artifactId >
40+ <version >1.2</version >
41+ </dependency >
42+ <dependency >
43+ <groupId >com.sun.faces</groupId >
44+ <artifactId >jsf-impl</artifactId >
45+ <version >2.2.8-02</version >
46+ </dependency >
47+
2248
2349 <dependency >
2450 <groupId >org.springframework.boot</groupId >
Original file line number Diff line number Diff line change 1+ package com .baeldung .springbootmvc .jsfapplication ;
2+
3+ import javax .faces .webapp .FacesServlet ;
4+
5+ import org .springframework .boot .SpringApplication ;
6+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
7+ import org .springframework .boot .web .servlet .ServletRegistrationBean ;
8+ import org .springframework .boot .web .servlet .support .SpringBootServletInitializer ;
9+ import org .springframework .context .annotation .Bean ;
10+ import org .springframework .context .annotation .ComponentScan ;
11+
12+ import com .baeldung .springbootmvc .jsfapplication .controller .JsfController ;
13+ import com .baeldung .springbootmvc .jsfapplication .model .TodoDao ;
14+ import com .baeldung .springbootmvc .jsfapplication .service .TodoService ;
15+
16+ @ SpringBootApplication
17+ @ ComponentScan (basePackageClasses = { JsfController .class , TodoDao .class , TodoService .class })
18+ public class JsfApplication extends SpringBootServletInitializer {
19+
20+ public static void main (String [] args ) {
21+ SpringApplication .run (JsfApplication .class , args );
22+ }
23+
24+ @ Bean
25+ public ServletRegistrationBean servletRegistrationBean () {
26+ FacesServlet servlet = new FacesServlet ();
27+ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (servlet , "*.jsf" );
28+ return servletRegistrationBean ;
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .springbootmvc .jsfapplication .controller ;
2+
3+ import org .springframework .context .annotation .Scope ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Scope (value = "session" )
7+ @ Component (value = "jsfController" )
8+ public class JsfController {
9+
10+ public String loadTodoPage () {
11+ checkPermission ();
12+ return "/todo.xhtml" ;
13+ }
14+
15+ private void checkPermission () {
16+ // Details omitted
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .springbootmvc .jsfapplication .model ;
2+
3+ import java .util .Collection ;
4+ import java .util .Optional ;
5+
6+ public interface Dao <T > {
7+
8+ Optional <T > get (int id );
9+
10+ Collection <T > getAll ();
11+
12+ int save (T t );
13+
14+ void update (T t );
15+
16+ void delete (T t );
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .springbootmvc .jsfapplication .model ;
2+
3+ public class Todo {
4+
5+ private int id ;
6+ private String message ;
7+ private int priority ;
8+
9+ public int getId () {
10+ return id ;
11+ }
12+
13+ public void setId (int id ) {
14+ this .id = id ;
15+ }
16+
17+ public String getMessage () {
18+ return message ;
19+ }
20+
21+ public void setMessage (String message ) {
22+ this .message = message ;
23+ }
24+
25+ public int getPriority () {
26+ return priority ;
27+ }
28+
29+ public void setPriority (int priority ) {
30+ this .priority = priority ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .springbootmvc .jsfapplication .model ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Collection ;
5+ import java .util .Collections ;
6+ import java .util .List ;
7+ import java .util .Objects ;
8+ import java .util .Optional ;
9+ import java .util .stream .Collectors ;
10+
11+ import org .springframework .stereotype .Component ;
12+
13+ @ Component
14+ public class TodoDao implements Dao <Todo > {
15+
16+ private List <Todo > todoList = new ArrayList <>();
17+
18+ @ Override
19+ public Optional <Todo > get (int id ) {
20+ return Optional .ofNullable (todoList .get (id ));
21+ }
22+
23+ @ Override
24+ public Collection <Todo > getAll () {
25+ return Collections .unmodifiableCollection (todoList .stream ()
26+ .filter (Objects ::nonNull )
27+ .collect (Collectors .toList ()));
28+ }
29+
30+ @ Override
31+ public int save (Todo todo ) {
32+ todoList .add (todo );
33+ int index = todoList .size () - 1 ;
34+ todo .setId (index );
35+ return index ;
36+ }
37+
38+ @ Override
39+ public void update (Todo todo ) {
40+ todoList .set (todo .getId (), todo );
41+ }
42+
43+ @ Override
44+ public void delete (Todo todo ) {
45+ todoList .set (todo .getId (), null );
46+ }
47+
48+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .springbootmvc .jsfapplication .service ;
2+
3+ import java .util .Collection ;
4+ import java .util .Comparator ;
5+ import java .util .stream .Collectors ;
6+
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .context .annotation .Scope ;
9+ import org .springframework .stereotype .Component ;
10+
11+ import com .baeldung .springbootmvc .jsfapplication .model .Dao ;
12+ import com .baeldung .springbootmvc .jsfapplication .model .Todo ;
13+
14+ @ Scope (value = "session" )
15+ @ Component (value = "todoService" )
16+ public class TodoService {
17+
18+ @ Autowired
19+ private Dao <Todo > todoDao ;
20+ private Todo todo = new Todo ();
21+
22+ public void save () {
23+ todoDao .save (todo );
24+ todo = new Todo ();
25+ }
26+
27+ public Collection <Todo > getAllTodo () {
28+ return todoDao .getAll ();
29+ }
30+
31+ public Collection <Todo > getAllTodoSortedByPriority () {
32+ return todoDao .getAll ()
33+ .stream ()
34+ .sorted (Comparator .comparingInt (Todo ::getId ))
35+ .collect (Collectors .toList ());
36+ }
37+
38+ public int saveTodo (Todo todo ) {
39+ validate (todo );
40+ return todoDao .save (todo );
41+ }
42+
43+ private void validate (Todo todo ) {
44+ // Details omitted
45+ }
46+
47+ public Todo getTodo () {
48+ return todo ;
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <faces-config xmlns =" http://xmlns.jcp.org/xml/ns/javaee"
3+ xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
5+ version=" 2.2" >
6+ <application >
7+ <el-resolver >org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver >
8+ </application >
9+ </faces-config >
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <web-app xmlns =" http://java.sun.com/xml/ns/javaee"
3+ xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
5+ http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6+ version=" 3.0" >
7+
8+ <servlet >
9+ <servlet-name >Faces Servlet</servlet-name >
10+ <servlet-class >javax.faces.webapp.FacesServlet</servlet-class >
11+ <load-on-startup >1</load-on-startup >
12+ </servlet >
13+ <servlet-mapping >
14+ <servlet-name >Faces Servlet</servlet-name >
15+ <url-pattern >*.jsf</url-pattern >
16+ </servlet-mapping >
17+ <context-param >
18+ <param-name >com.sun.faces.forceLoadConfiguration</param-name >
19+ <param-value >true</param-value >
20+ </context-param >
21+ </web-app >
Original file line number Diff line number Diff line change 1+ < f:view xmlns ="http://www.w3c.org/1999/xhtml "
2+ xmlns:f ="http://java.sun.com/jsf/core "
3+ xmlns:h ="http://java.sun.com/jsf/html ">
4+ < h:head >
5+ < meta charset ="utf-8 "/>
6+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge,chrome=1 "/>
7+ < title > TO-DO application</ title >
8+ </ h:head >
9+ < h:body >
10+ < div >
11+ < p > Welcome in the TO-DO application!</ p >
12+ < p style ="height:50px ">
13+ This is a static message rendered from xhtml.
14+ < h:form >
15+ < h:commandButton value ="Load To-do page! " action ="#{jsfController.loadTodoPage} " />
16+ </ h:form >
17+ </ p >
18+ </ div >
19+ </ h:body >
20+ </ f:view >
You can’t perform that action at this time.
0 commit comments