在 Java Web 开发中,Spring 框架是一个非常强大且流行的选择。视图技术是 Web 应用开发中的重要组成部分,它负责将模型数据呈现给用户。JSP(JavaServer Pages)作为一种传统且广泛使用的视图技术,与 Spring 框架结合使用可以很好地实现动态网页的开发。本文将详细介绍如何在 Spring 项目中使用 JSP 作为视图。
首先,我们需要创建一个基于 Spring 的 Web 项目。这里我们使用 Maven 来管理项目依赖。以下是 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.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-jsp-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring Web MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
上述 pom.xml
文件中引入了 Spring Web MVC、Servlet API 和 JSP API 的依赖。
接下来,我们需要配置 Spring MVC,使其支持 JSP 视图。创建一个 Spring 配置类 WebConfig
:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controller")
public class WebConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
在上述代码中,我们使用 InternalResourceViewResolver
作为视图解析器,将视图的前缀设置为 /WEB-INF/views/
,后缀设置为 .jsp
。这样,当控制器返回视图名称时,Spring 会自动在 /WEB-INF/views/
目录下查找对应的 JSP 文件。
为了启动 Spring MVC 应用,我们需要创建一个 Servlet 初始化类 WebAppInitializer
:
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// 创建 Spring 应用上下文
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebConfig.class);
// 添加 ContextLoaderListener
servletContext.addListener(new ContextLoaderListener(context));
// 注册 DispatcherServlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
WebAppInitializer
类实现了 WebApplicationInitializer
接口,用于在 Servlet 容器启动时初始化 Spring MVC 应用。
创建一个简单的控制器 HelloController
:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello, JSP with Spring!");
return "hello";
}
}
在上述代码中,@GetMapping("/hello")
注解表示处理 /hello
请求,将消息 "Hello, JSP with Spring!"
添加到模型中,并返回视图名称 hello
。
在 /WEB-INF/views/
目录下创建 hello.jsp
文件:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello JSP</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
在 hello.jsp
文件中,我们使用 EL 表达式 ${message}
来显示控制器中添加到模型的消息。
将项目部署到 Servlet 容器(如 Tomcat)中,启动容器后,访问 http://localhost:8080/hello
,你将看到页面上显示 Hello, JSP with Spring!
。
步骤 | 描述 |
---|---|
1. 项目环境搭建 | 使用 Maven 引入 Spring Web MVC、Servlet API 和 JSP API 的依赖 |
2. 配置 Spring MVC | 创建 Spring 配置类,使用 InternalResourceViewResolver 作为视图解析器 |
3. 创建 Servlet 初始化类 | 实现 WebApplicationInitializer 接口,初始化 Spring MVC 应用 |
4. 创建控制器 | 使用 @Controller 注解创建控制器,处理请求并返回视图名称 |
5. 创建 JSP 视图 | 在 /WEB-INF/views/ 目录下创建 JSP 文件,使用 EL 表达式显示模型数据 |
6. 运行项目 | 将项目部署到 Servlet 容器中,访问相应的 URL 查看结果 |
通过以上步骤,我们成功地在 Spring 项目中使用 JSP 作为视图。JSP 与 Spring 框架的结合可以方便地实现动态网页的开发,为用户提供丰富的交互体验。