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

}

}

相关推荐
Csvn4 小时前
Monorepo
前端
Csvn4 小时前
流水线
前端
Csvn4 小时前
微前端架构
前端
Linux-lucky6 小时前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu
风骏时光牛马8 小时前
生成一个移动端标题,只需要一个标题,不需要其他的任何文本。
前端
IT_陈寒9 小时前
Redis雪崩把我坑惨了,这是5年经验的教训
前端·人工智能·后端
SameX9 小时前
HarmonyOS ArkUI List 的侧滑删除 用 ListItem 的 swipeAction + endIcon 的做法
前端
露天赏雪9 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
usdoc文档预览11 小时前
USDOC系列之PPT文件在线编辑-VUE前端实现在线浏览器在线编辑PPT文档
前端·vue.js·编辑器·office365·ppt在线编辑·office文件在线编辑·文件在线编辑
用户27050777252411 小时前
Agent 调工具卡住?3 种超时与取消策略实测对比
前端