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);

}

}

相关推荐
大模型铲屎官21 分钟前
HTML5 技术深度解读:本地存储与地理定位的最佳实践
前端·html·html5·本地存储·localstorage·地理定位·geolocation api
一 乐1 小时前
基于vue船运物流管理系统设计与实现(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端·船运系统
m0_528723811 小时前
在React中使用redux
前端·javascript·react.js
傻小胖1 小时前
vue3中customRef的用法以及使用场景
前端·javascript·vue.js
谦谦橘子1 小时前
手把手教你实现一个富文本
前端·javascript
Future_yzx2 小时前
Java Web的发展史与SpringMVC入门学习(SpringMVC框架入门案例)
java·前端·学习
star010-2 小时前
【视频+图文详解】HTML基础4-html标签的基本使用
前端·windows·经验分享·网络安全·html·html5
engchina2 小时前
CSS Display属性完全指南
前端·css
engchina2 小时前
详解CSS `clear` 属性及其各个选项
前端·css·css3
lwprain3 小时前
springboot 2.7.6 security mysql redis jwt配置例子
spring boot·redis·mysql