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

}

}

相关推荐
问心无愧051311 小时前
ctf show web入门160 161
前端·笔记
李小白6611 小时前
第四天-WEB服务器基本原理,IIS服务
运维·服务器·前端
humcomm12 小时前
AI编程时代新前端职位
前端·ai编程
好家伙VCC12 小时前
Web Components主题热切换方案揭秘
java·前端
甲维斯13 小时前
Kimi版超级玛丽效果“惊人”,配额不足5厘米!
前端·人工智能
hboot13 小时前
AI工程师第一课 - Python
前端·后端·python
凉菜凉凉13 小时前
AI时代,被抛弃的前端
前端·ai
console.log('npc')13 小时前
AI前端工程与生成式UI学习路线
前端·人工智能·ui
梦曦i13 小时前
uni-router v1.1.1发布:守卫超时保护+路由监听
前端·uni-app
qq_25183645714 小时前
基于java Web网络订餐系统设计与实现 源码文档
java·开发语言·前端