Merge pull request #5304 from Vizsoro/controller-service-dao-jsf-2
SpringBoot- service, controller, dao with JSF
This commit is contained in:
commit
49bcb08b44
|
@ -19,6 +19,32 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--JSF-->
|
||||
<dependency>
|
||||
<groupId>com.sun.faces</groupId>
|
||||
<artifactId>jsf-api</artifactId>
|
||||
<version>2.2.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.faces</groupId>
|
||||
<artifactId>javax.faces-api</artifactId>
|
||||
<version>2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.faces</groupId>
|
||||
<artifactId>jsf-impl</artifactId>
|
||||
<version>2.2.8-02</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.springbootmvc.jsfapplication;
|
||||
|
||||
import javax.faces.webapp.FacesServlet;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import com.baeldung.springbootmvc.jsfapplication.controller.JsfController;
|
||||
import com.baeldung.springbootmvc.jsfapplication.model.TodoDao;
|
||||
import com.baeldung.springbootmvc.jsfapplication.service.TodoService;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { JsfController.class, TodoDao.class, TodoService.class })
|
||||
public class JsfApplication extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(JsfApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean servletRegistrationBean() {
|
||||
FacesServlet servlet = new FacesServlet();
|
||||
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
|
||||
return servletRegistrationBean;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.springbootmvc.jsfapplication.controller;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Scope(value = "session")
|
||||
@Component(value = "jsfController")
|
||||
public class JsfController {
|
||||
|
||||
public String loadTodoPage() {
|
||||
checkPermission();
|
||||
return "/todo.xhtml";
|
||||
}
|
||||
|
||||
private void checkPermission() {
|
||||
// Details omitted
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.springbootmvc.jsfapplication.model;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface Dao<T> {
|
||||
|
||||
Optional<T> get(int id);
|
||||
|
||||
Collection<T> getAll();
|
||||
|
||||
int save(T t);
|
||||
|
||||
void update(T t);
|
||||
|
||||
void delete(T t);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.springbootmvc.jsfapplication.model;
|
||||
|
||||
public class Todo {
|
||||
|
||||
private int id;
|
||||
private String message;
|
||||
private int priority;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public void setPriority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.springbootmvc.jsfapplication.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TodoDao implements Dao<Todo> {
|
||||
|
||||
private List<Todo> todoList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Optional<Todo> get(int id) {
|
||||
return Optional.ofNullable(todoList.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Todo> getAll() {
|
||||
return Collections.unmodifiableCollection(todoList.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int save(Todo todo) {
|
||||
todoList.add(todo);
|
||||
int index = todoList.size() - 1;
|
||||
todo.setId(index);
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Todo todo) {
|
||||
todoList.set(todo.getId(), todo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Todo todo) {
|
||||
todoList.set(todo.getId(), null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.springbootmvc.jsfapplication.service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.springbootmvc.jsfapplication.model.Dao;
|
||||
import com.baeldung.springbootmvc.jsfapplication.model.Todo;
|
||||
|
||||
@Scope(value = "session")
|
||||
@Component(value = "todoService")
|
||||
public class TodoService {
|
||||
|
||||
@Autowired
|
||||
private Dao<Todo> todoDao;
|
||||
private Todo todo = new Todo();
|
||||
|
||||
public void save() {
|
||||
todoDao.save(todo);
|
||||
todo = new Todo();
|
||||
}
|
||||
|
||||
public Collection<Todo> getAllTodo() {
|
||||
return todoDao.getAll();
|
||||
}
|
||||
|
||||
public Collection<Todo> getAllTodoSortedByPriority() {
|
||||
return todoDao.getAll()
|
||||
.stream()
|
||||
.sorted(Comparator.comparingInt(Todo::getId))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public int saveTodo(Todo todo) {
|
||||
validate(todo);
|
||||
return todoDao.save(todo);
|
||||
}
|
||||
|
||||
private void validate(Todo todo) {
|
||||
// Details omitted
|
||||
}
|
||||
|
||||
public Todo getTodo() {
|
||||
return todo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
|
||||
version="2.2">
|
||||
<application>
|
||||
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
|
||||
</application>
|
||||
</faces-config>
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
version="3.0">
|
||||
|
||||
<servlet>
|
||||
<servlet-name>Faces Servlet</servlet-name>
|
||||
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>Faces Servlet</servlet-name>
|
||||
<url-pattern>*.jsf</url-pattern>
|
||||
</servlet-mapping>
|
||||
<context-param>
|
||||
<param-name>com.sun.faces.forceLoadConfiguration</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
</web-app>
|
|
@ -0,0 +1,20 @@
|
|||
<f:view xmlns="http://www.w3c.org/1999/xhtml"
|
||||
xmlns:f="http://java.sun.com/jsf/core"
|
||||
xmlns:h="http://java.sun.com/jsf/html">
|
||||
<h:head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
|
||||
<title>TO-DO application</title>
|
||||
</h:head>
|
||||
<h:body>
|
||||
<div>
|
||||
<p>Welcome in the TO-DO application!</p>
|
||||
<p style="height:50px">
|
||||
This is a static message rendered from xhtml.
|
||||
<h:form>
|
||||
<h:commandButton value="Load To-do page!" action="#{jsfController.loadTodoPage}" />
|
||||
</h:form>
|
||||
</p>
|
||||
</div>
|
||||
</h:body>
|
||||
</f:view>
|
|
@ -0,0 +1,38 @@
|
|||
<f:view xmlns="http://www.w3c.org/1999/xhtml"
|
||||
xmlns:f="http://java.sun.com/jsf/core"
|
||||
xmlns:h="http://java.sun.com/jsf/html">
|
||||
<h:head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
|
||||
<title>TO-DO application</title>
|
||||
</h:head>
|
||||
<h:body>
|
||||
<div>
|
||||
<div>
|
||||
List of TO-DO items
|
||||
</div>
|
||||
<h:dataTable value="#{todoService.allTodo}" var="item">
|
||||
<h:column>
|
||||
<f:facet name="header"> Message</f:facet>
|
||||
#{item.message}
|
||||
</h:column>
|
||||
<h:column>
|
||||
<f:facet name="header"> Priority</f:facet>
|
||||
#{item.priority}
|
||||
</h:column>
|
||||
</h:dataTable>
|
||||
</div>
|
||||
<div>
|
||||
<div>
|
||||
Add new to-do item:
|
||||
</div>
|
||||
<h:form>
|
||||
<h:outputLabel for="message" value="Message: "/>
|
||||
<h:inputText id="message" value="#{todoService.todo.message}"/>
|
||||
<h:outputLabel for="priority" value="Priority: "/>
|
||||
<h:inputText id="priority" value="#{todoService.todo.priority}" converterMessage="Please enter digits only."/>
|
||||
<h:commandButton value="Save" action="#{todoService.save}"/>
|
||||
</h:form>
|
||||
</div>
|
||||
</h:body>
|
||||
</f:view>
|
Loading…
Reference in New Issue