设为首页 加入收藏

TOP

day07-SpringMVC底层机制简单实现-03(三)
2023-07-26 08:16:17 】 浏览:142
Tags:day07-SpringMVC 简单实 -03
pache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgs> <arg>-parameters</arg> </compilerArgs> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build>

(4)编写方法测试

MonsterService 接口:

package com.li.service;

import com.li.entity.Monster;

import java.util.List;

/**
 * @author 李
 * @version 1.0
 */
public interface MonsterService {
    //增加方法,通过传入的名字返回 monster列表
    public List<Monster> findMonsterByName(String name);
}

MonsterServiceImpl 实现类:

package com.li.service.impl;

import com.li.entity.Monster;
import com.li.myspringmvc.annotation.Service;
import com.li.service.MonsterService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李
 * @version 1.0
 * MonsterServiceImpl 作为一个Service对象注入容器
 */
@Service
public class MonsterServiceImpl implements MonsterService {
    @Override
    public List<Monster> findMonsterByName(String name) {
        //这里模拟到 DB获取数据
        List<Monster> monsters = new ArrayList<>();
        monsters.add(new Monster(100, "牛魔王", "芭蕉扇", 400));
        monsters.add(new Monster(200, "猫妖", "撕咬", 800));
        monsters.add(new Monster(300, "鼠精", "偷灯油", 200));
        monsters.add(new Monster(400, "大象精", "运木头", 300));
        monsters.add(new Monster(500, "白骨精", "吐烟雾", 500));

        //创建集合返回查询到的monster集合
        List<Monster> findMonsters = new ArrayList<>();
        //遍历monster集合,将符合条件的放到findMonster集合中
        for (Monster monster : monsters) {
            if (monster.getName().contains(name)) {
                findMonsters.add(monster);
            }
        }
        return findMonsters;
    }
}

MonsterController 控制器类:

package com.li.controller;

import com.li.entity.Monster;
import com.li.myspringmvc.annotation.AutoWired;
import com.li.myspringmvc.annotation.Controller;
import com.li.myspringmvc.annotation.RequestMapping;
import com.li.myspringmvc.annotation.RequestParam;
import com.li.service.MonsterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 李
 * @version 1.0
 * 用于测试的 Controller
 */
@Controller
public class MonsterController {
    //属性
    @AutoWired
    private MonsterService monsterService;

    //增加方法,通过name返回对应的monster集合
    @RequestMapping(value = "/monster/find")
    public void findMonsterByName(HttpServletRequest request, 
                                  HttpServletResponse response,
                                  @RequestParam(value = "name") String monsterName) {
        //设置编码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("----接收到的name=" + monsterName);
        StringBuilder content = new StringBuilder("<h1>妖怪列表信息</h1>");
        content.append("<table border='1px' width='400px' style='border-collapse:collapse'>");
        //调用 monsterService的方法
        List<Monster> monsters = monsterService.findMonsterByName(monsterName);
        for (Monster monst
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇《分布式技术原理与算法解析》学.. 下一篇day01-2-@RequestMapping和Rest

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目