注解版
- 新建一个子项目,添加Web支持
- 在pom.xml文件中引入相关的依赖:主要引入Spring框架核心库、SpringMVC、servlet,JSTL等。
- 创建实体类Fruit
| package com.pp.pojo; |
| |
| import lombok.AllArgsConstructor; |
| import lombok.Builder; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| |
| @Data |
| @Builder |
| @AllArgsConstructor |
| @NoArgsConstructor |
| public class Fruit { |
| private String name; |
| private double price; |
| private String producing_area; |
| |
| } |
| |
- 编写实体类对应的Controller
| package com.pp.controller; |
| |
| import com.pp.pojo.Fruit; |
| import com.pp.service.FruitService; |
| 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 java.util.List; |
| |
| @Controller |
| public class FruitController { |
| |
| @Autowired |
| FruitService fruitService; |
| @RequestMapping("/fc") |
| public String getFruit(Model model){ |
| |
| List<Fruit> fruitLists=fruitService.queryFruitList(); |
| model.addAttribute("fruitList",fruitLists); |
| return "fruitList"; |
| } |
| |
| } |
| |
- 配置SpringMVC,类似于Springmvc.xml
| package com.pp.config; |
| |
| import org.springframework.context.annotation.Bean; |
| import org.springframework.context.annotation.ComponentScan; |
| import org.springframework.context.annotation.Configuration; |
| import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; |
| import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| import org.springframework.web.servlet.view.InternalResourceViewResolver; |
| |
| @Configuration |
| @EnableWebMvc |
| @ComponentScan("com.pp") |
| |
| public class SpringMVCConfig implements WebMvcConfigurer { |
| |
| |
| @Bean |
| public InternalResourceViewResolver getViewResolver(){ |
| InternalResourceViewResolver viewResolver = new |
| InternalResourceViewResolver(); |
| viewResolver.setPrefix("/WEB-INF/jsp/"); |
| viewResolver.setSuffix(".jsp"); |
| return viewResolver; |
| } |
| |
| @Override |
| public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { |
| configurer.enable(); |
| } |
| |
| } |
| |
- 配置WebConfig,原来的web.xml
这里注意别导错包
import javax.servlet.ServletRegistration.Dynamic;
一定是这两个!不然会报错
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>javax.servlet-api</artifactId> |
| <version>4.0.1</version> |
| <scope>provided</scope> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>jstl</artifactId> |
| <version>1.2</version> |
| </dependency> |
| package com.pp.config; |
| |
| import org.springframework.web.WebApplicationInitializer; |
| 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.Dynamic; |
| |
| |
| public class WebConfig implements WebApplicationInitializer { |
| @Override |
| public void onStartup(ServletContext servletContext) throws ServletException { |
| |
| AnnotationConfigWebApplicationContext ctx = new |
| AnnotationConfigWebApplicationContext(); |
| ctx.register(SpringMVCConfig.class); |
| ctx.setServletContext(servletContext); |
| ctx.refresh(); |
| |
| Dynamic servlet = servletContext.addServlet("dispatcher", new |
| DispatcherServlet(ctx)); |
| servlet.addMapping("/"); |
| servlet.setLoadOnStartup(1); |
| } |
| |
| } |
| |
| |
到这里,以前的xml文件就完全被我们的Config给取代了!!!有没有很爽!!
接下来编写Service
- FruitService类
| package com.pp.service; |
| |
| import com.pp.pojo.Fruit; |
| |
| import java.util.List; |
| |
| public interface FruitService { |
| List<Fruit> queryFruitList(); |
| |
| } |
| |
对应的实现类
| package com.pp.service; |
| |
| import com.pp.pojo.Fruit; |
| import org.springframework.stereotype.Service; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| @Service |
| public class FruitServiceImpl implements FruitService{ |
| @Override |
| public List<Fruit> queryFruitList() { |
| List<Fruit> fruitsList = new ArrayList<>(); |
| Fruit apple =Fruit.builder() |
| .name("苹果") |
| .price(19) |
| .producing_area("山东烟台") |
| .build(); |
| Fruit banana =Fruit.builder() |
| .name("香蕉") |
| .price(5) |
| .producing_area("海南") |
| .build(); |
| Fruit pear =Fruit.builder() |
| .name("梨子") |
| .price(8.5) |
| .producing_area("汤山") |
| .build(); |
| fruitsList.add(apple); |
| fruitsList.add(pear); |
| fruitsList.add(banana); |
| return fruitsList; |
| } |
| } |
| |
- 编写jsp视图层!在WEB-INF/jsp下创建fruitList.jsp
| <%-- |
| Created by IntelliJ IDEA. |
| User: supanpan199919 |
| Date: 2021-05-08 |
| Time: 19:55 |
| To change this template use File | Settings | File Templates. |
| --%> |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> |
| <html> |
| <head> |
| <title>Title</title> |
| </head> |
| <body> |
| <h2>水果信息情况</h2> |
| <table border="1px" width="400px"> |
| <thead> |
| <th>名称</th> |
| <th>价格</th> |
| <th>产地</th> |
| </thead> |
| <tbody> |
| <c:forEach items="${fruitList}" var="fruit"> |
| <tr> |
| <td>${fruit.name}</td> |
| <td>${fruit.price}</td> |
| <td>${fruit.producing_area}</td> |
| </tr> |
| </c:forEach> |
| </tbody> |
| <tfoot> |
| <tr> |
| <td colspan="3" align="center">@copy软件班</td> |
| </tr> |
| </tfoot> |
| </table> |
| |
| </body> |
| </html> |
| |
最后!!!配置tom,启动测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南