微信登录

视图技术 - JSP 视图 - 使用 JSP 作为视图

Java Web Spring:使用 JSP 作为视图

在 Java Web 开发中,Spring 框架是一个非常强大且流行的选择。视图技术是 Web 应用开发中的重要组成部分,它负责将模型数据呈现给用户。JSP(JavaServer Pages)作为一种传统且广泛使用的视图技术,与 Spring 框架结合使用可以很好地实现动态网页的开发。本文将详细介绍如何在 Spring 项目中使用 JSP 作为视图。

1. 项目环境搭建

首先,我们需要创建一个基于 Spring 的 Web 项目。这里我们使用 Maven 来管理项目依赖。以下是 pom.xml 文件的示例:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>spring-jsp-demo</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <properties>
  10. <maven.compiler.source>1.8</maven.compiler.source>
  11. <maven.compiler.target>1.8</maven.compiler.target>
  12. </properties>
  13. <dependencies>
  14. <!-- Spring Web MVC -->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-webmvc</artifactId>
  18. <version>5.3.18</version>
  19. </dependency>
  20. <!-- Servlet API -->
  21. <dependency>
  22. <groupId>javax.servlet</groupId>
  23. <artifactId>javax.servlet-api</artifactId>
  24. <version>4.0.1</version>
  25. <scope>provided</scope>
  26. </dependency>
  27. <!-- JSP API -->
  28. <dependency>
  29. <groupId>javax.servlet.jsp</groupId>
  30. <artifactId>javax.servlet.jsp-api</artifactId>
  31. <version>2.3.3</version>
  32. <scope>provided</scope>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <plugins>
  37. <plugin>
  38. <groupId>org.apache.maven.plugins</groupId>
  39. <artifactId>maven-war-plugin</artifactId>
  40. <version>3.3.2</version>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

上述 pom.xml 文件中引入了 Spring Web MVC、Servlet API 和 JSP API 的依赖。

2. 配置 Spring MVC

接下来,我们需要配置 Spring MVC,使其支持 JSP 视图。创建一个 Spring 配置类 WebConfig

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.ViewResolver;
  5. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  6. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  7. @Configuration
  8. @EnableWebMvc
  9. @ComponentScan(basePackages = "com.example.controller")
  10. public class WebConfig {
  11. @Bean
  12. public ViewResolver viewResolver() {
  13. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  14. resolver.setPrefix("/WEB-INF/views/");
  15. resolver.setSuffix(".jsp");
  16. return resolver;
  17. }
  18. }

在上述代码中,我们使用 InternalResourceViewResolver 作为视图解析器,将视图的前缀设置为 /WEB-INF/views/,后缀设置为 .jsp。这样,当控制器返回视图名称时,Spring 会自动在 /WEB-INF/views/ 目录下查找对应的 JSP 文件。

3. 创建 Servlet 初始化类

为了启动 Spring MVC 应用,我们需要创建一个 Servlet 初始化类 WebAppInitializer

  1. import org.springframework.web.WebApplicationInitializer;
  2. import org.springframework.web.context.ContextLoaderListener;
  3. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  4. import org.springframework.web.servlet.DispatcherServlet;
  5. import javax.servlet.ServletContext;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRegistration;
  8. public class WebAppInitializer implements WebApplicationInitializer {
  9. @Override
  10. public void onStartup(ServletContext servletContext) throws ServletException {
  11. // 创建 Spring 应用上下文
  12. AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
  13. context.register(WebConfig.class);
  14. // 添加 ContextLoaderListener
  15. servletContext.addListener(new ContextLoaderListener(context));
  16. // 注册 DispatcherServlet
  17. ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
  18. dispatcher.setLoadOnStartup(1);
  19. dispatcher.addMapping("/");
  20. }
  21. }

WebAppInitializer 类实现了 WebApplicationInitializer 接口,用于在 Servlet 容器启动时初始化 Spring MVC 应用。

4. 创建控制器

创建一个简单的控制器 HelloController

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.ui.Model;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. @Controller
  5. public class HelloController {
  6. @GetMapping("/hello")
  7. public String hello(Model model) {
  8. model.addAttribute("message", "Hello, JSP with Spring!");
  9. return "hello";
  10. }
  11. }

在上述代码中,@GetMapping("/hello") 注解表示处理 /hello 请求,将消息 "Hello, JSP with Spring!" 添加到模型中,并返回视图名称 hello

5. 创建 JSP 视图

/WEB-INF/views/ 目录下创建 hello.jsp 文件:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Hello JSP</title>
  7. </head>
  8. <body>
  9. <h1>${message}</h1>
  10. </body>
  11. </html>

hello.jsp 文件中,我们使用 EL 表达式 ${message} 来显示控制器中添加到模型的消息。

6. 运行项目

将项目部署到 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 框架的结合可以方便地实现动态网页的开发,为用户提供丰富的交互体验。

视图技术 - JSP 视图 - 使用 JSP 作为视图