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

}

}

相关推荐
excel40 分钟前
watchEffect 为什么会执行两次?
前端
Java展博13 小时前
[前端够用就好] 跨域报错时,浏览器到底在拦谁
前端·状态模式
用户1129361121914 小时前
npm 都提示成功了,node_modules 为什么还占着 370M?
linux·前端·后端
掘金安东尼15 小时前
Agent Harness 也有 benchmark:评分榜与评测工作台现状
前端·agent
digger_41915 小时前
从零理解 Electron:主进程、渲染进程、preload 与 IPC 实战
前端·electron
去伪存真16 小时前
别让你的 RAG 平台“花五份钱、答一份结巴的题”:幂等性架构的自我修养
前端·agent
snow@li16 小时前
前端:导入EXCEL并解析文件内容,demo
前端·javascript·vue.js
考虑考虑17 小时前
Spingboot4.2.0中的属性
spring boot·后端·spring
补码缩写17 小时前
压缩PNG不损失透明:只量化RGB、alpha原样留的减色流程
前端
RuoyiOffice17 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office