之前研究学习Spring MVC的时候总是用JSP作为视图,偶尔也尝试过Thymleaf,学习Java EE开发的时候又尝试过JSF,之前也翻译过一篇有关Spring Boot和JSF整合的文章——请参阅用Spring Boot开发JSF应用程序(翻译)。在此,对利用JSF作为视图进行Spring MVC开发进行详细的说明,从最开始的工程构建到实例的运行进行分享。这里只是把JSP简单替换成为JSF,如果比较熟悉的话,可以进一步看看尝试利用JSF作为Spring MVC的视图(探索篇)之表格的排序与选择
首先介绍一下开发环境:
下面开始按照开发步骤进行说明,也作为自己的备忘。
首先需要创建Maven工程并转换为Web工程。
参考下面的步骤,创建一个简单的Maven工程。
设置工程的信息,点击Finish。
编辑pom.xml
文件。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.study.basicgroupId>
<artifactId>SpringJSFBasicartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>utf-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
<version>9.0.14version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.apache.myfaces.coregroupId>
<artifactId>myfaces-apiartifactId>
<version>2.3.2version>
dependency>
<dependency>
<groupId>org.apache.myfaces.coregroupId>
<artifactId>myfaces-implartifactId>
<version>2.3.2version>
dependency>
<dependency>
<groupId>org.primefacesgroupId>
<artifactId>primefacesartifactId>
<version>6.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.4version>
<optional>trueoptional>
dependency>
dependencies>
project>
刷新Maven工程,用Alt + F5
,或者右键菜单。
工程会转换成Web工程,但是会提示缺少web.xml
文件。
利用右键菜单生成web.xml
文件,如下图:
编辑web.xml
文件的内容如下:
注意: 这里使用了默认设置,并没有指定具体的文件路径。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>SpringJSFBasicdisplay-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGEparam-name>
<param-value>Developmentparam-value>
context-param>
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
<welcome-file-list>
<welcome-file>index.xhtmlwelcome-file>
welcome-file-list>
web-app>
在/WEB-INF
路径下添加applicationContext.xml
和mvc-servlet.xml
,内容如下:
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
<context:component-scan
base-package="org.study.basic.service" />
beans>
mvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<mvc:annotation-driven />
<context:component-scan
base-package="org.study.basic.mvc" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".xhtml" />
bean>
beans>
接着是添加如下的代码文件,比较简单,不需要详细说明。关键是利用lombok
的方法,可以参照相关的文章。
Student.java
package org.study.basic.repository.data;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private long id;
private String name;
private int age;
private String description;
}
StudentService.java
package org.study.basic.service;
import java.util.List;
import org.study.basic.repository.data.Student;
public interface StudentService {
Student findStudentById(long id);
List<Student> findAllStudents();
}
StudentServiceImpl.java
package org.study.basic.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.study.basic.repository.data.Student;
import org.study.basic.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
private Map<Long, Student> mapStudent;
public StudentServiceImpl() {
this.initForTest();
}
@Override
public Student findStudentById(long id) {
return this.mapStudent.get(id);
}
@Override
public List<Student> findAllStudents() {
return new ArrayList<Student>(this.mapStudent.values());
}
private void initForTest() {
this.mapStudent = new HashMap<Long, Student>();
this.mapStudent.put(1L, new Student(1, "学生一", 25, "擅长前端开发"));
this.mapStudent.put(2L, new Student(2, "学生二", 26, "擅长后端开发"));
this.mapStudent.put(3L, new Student(3, "学生三", 24, "擅长算法设计"));
}
}
StudentController .java
package org.study.basic.mvc.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.study.basic.repository.data.Student;
import org.study.basic.service.StudentService;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String showInfo(Model model) {
Student student = this.studentService.findStudentById(2);
model.addAttribute("student", student);
List<Student> students = this.studentService.findAllStudents();
model.addAttribute("students", students);
return "showInfo";
}
}
最后在/WEB-INF/views
路径下添加一个简单的showInfo.xhtml
文件,内容如下:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>h:head>
<h:body>
<h2>最佳学生:h2>
<h:outputText value="${student.name}" />
<h2>全体学生:h2>
<p:dataTable value="${students}" var="student" selectionMode="single"
rowKey="${student.id}">
<p:column headerText="姓名">
<h:outputText value="${student.name}" />
p:column>
<p:column headerText="年龄">
<h:outputText value="${student.age}" />
p:column>
<p:column headerText="特长">
<h:outputText value="${student.description}" />
p:column>
p:dataTable>
h:body>
html>
最后的工程结构如下图:
部署工程并运行。
访问http://127.0.0.1:8080/SpringJSFBasic/student/info
,效果如下:
有关JSF的高级话题请关注后续文章。