Java web第四次作业

要求:读取xml文件并在页面中显示出来。

采用三种方式实现,并体会其中的原理:

1.常规方式,controlller控制器不分层

代码:@RestController

public class PoetController {

@RequestMapping("/listPoet")

public Result list(){

String file = this.getClass().getClassLoader().getResource("poet.xml").getFile();

System.out.println(file);

List<Poet> poetList= XmlParserUtils.parse(file, Poet.class);

poetList.stream().forEach(poet ->{

String gender=poet.getGender;

if("1".equals(gender)){

poet.setGender("男");

}

else if("2".equals(gender)){

poet.setGender("女");

}

});

return Result.success(poetList);

}

}

2.按照MVC的分层方式实现,常规java代码方式

3.采用控制反转和依赖注入的MVC方式实现。

代码:@Component

public class PoetDaoA implements PoetDao {

@Override

public List<Poet> listPoet() {

String file=this.getClass().getClassLoader().getResource("poet.xml").getFile();

System.out.println(file);

List<Poet> poetList= XmlParserUtils.parse(file,Poet.class);

System.out.println(poetList);

return poetList;

}

}

@Component

public class PoetServiceA implements PoetService {

@Autowired

private PoetDao poetDao;

@Override

public List<Poet> listPoet() {

List<Poet> poetList=poetDao.listPoet();

poetList.stream().forEach(poet -> {

String gender = poet.getGender;

if ("1".equals(gender)) {

poet.setGender("男");

} else if ("2".equals(gender)) {

poet.setGender("女");

}

});

return poetList;

}

}

@RestController

public class PoetController {

private PoetService poetService;

@RequestMapping("/poetlist2")

public Result list(){

List<Poet> poetList=poetService.listPoet();

return Result.success(poetList);

}

}

相关推荐
swipe4 小时前
05|(前端转后全栈)不手写一堆 SQL,后端怎么操作数据库?MyBatis-Plus 入门
前端·后端·全栈
霸道流氓气质4 小时前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
swipe4 小时前
04|(前端转后全栈)前端状态为什么不够用?从页面数据到 MySQL 持久化
前端·后端·全栈
橘子星4 小时前
别光看教程!手把手拆解 React Todo 项目,一文吃透 5 个核心概念
前端·javascript
大白要努力!4 小时前
纯前端实现 PDF 加水印工具 —— 零后端、支持中文、实时预览
前端·pdf·html
石小石Orz4 小时前
TRAE SOLO实战:实现一个桌面3D助手
前端·人工智能
蜡台4 小时前
使用 uni-popup 实现数据选择器Data-Picker
前端·javascript·html·uniapp·uni-popup·data-picker
拆房老料4 小时前
BaseMetas FileView 1.2.0 发布:Office/WPS 大文件预览与内存安全优化实测
前端·产品运营·开源软件
blns_yxl4 小时前
Promise封装Fetch + 重试机制(HTML+JS)
前端·javascript·html
Elastic 中国社区官方博客4 小时前
使用重新设计的 AutoOps 更快地进行 Elasticsearch 问题排查
大数据·运维·前端·人工智能·elasticsearch·搜索引擎·全文检索