atwood-pan

 

SpringMVC02——第一个MVC程序-注解版(high版!!!!)

注解版

  1. 新建一个子项目,添加Web支持
  2. 在pom.xml文件中引入相关的依赖:主要引入Spring框架核心库、SpringMVC、servlet,JSTL等。
  3. 创建实体类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;//产地
}
  1. 编写实体类对应的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){
//模拟Service获取水果商品列表
List<Fruit> fruitLists=fruitService.queryFruitList();
model.addAttribute("fruitList",fruitLists);
return "fruitList";
}
}
  1. 配置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 {
//配置jsp视图解析器
@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();
}
}
  1. 配置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 {
// 加载Spring web用于配置
AnnotationConfigWebApplicationContext ctx = new
AnnotationConfigWebApplicationContext();
ctx.register(SpringMVCConfig.class);
ctx.setServletContext(servletContext);
ctx.refresh();
// 创建dispatcherServlet
Dynamic servlet = servletContext.addServlet("dispatcher", new
DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}

到这里,以前的xml文件就完全被我们的Config给取代了!!!有没有很爽!!
接下来编写Service

  1. 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;
}
}
  1. 编写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,启动测试

posted on   JavaCoderPan  阅读(15)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

统计

点击右上角即可分享
微信分享提示