簡體   English   中英

spring requestmapping 測試代碼不起作用

[英]spring requestmapping testing code doesn't works

我正在研究 spring 5,我不能使用 @RequestMapping 注釋,不知道為什么

@RequestMapping 包括 @Component 注釋所以我只是想我可以使用它

initRequest 按字符串包含 URL 參數

我只是期望 initRequest(/hello) 參數綁定 URL

這是我的代碼

public class SimpleControllerTest extends AbstractDispatcherServletTest {
@Test
public void helloSimpleController() throws ServletException, IOException {
    setClasses(HelloController.class);
    initRequest("/hello").addParameter("name", "spring");
    runService();
    assertModel("message", "Hello spring");
    assertViewName("/WEB-INF/view/hello.jsp");
}

@Test(expected=Exception.class)
public void noParameterHelloSimpleController() throws ServletException, IOException {
    setClasses(HelloController.class);
    initRequest("/hello");
    runService();
}

@Component("/hello")
//@RequestMapping("/hello")
static class HelloController extends SimpleController {
    public HelloController() {
        this.setRequiredParams(new String[] {"name"});
        this.setViewName("/WEB-INF/view/hello.jsp");
    }

    public void control(Map<String, String> params, Map<String, Object> model) throws Exception {
        model.put("message", "Hello " + params.get("name"));
    }
}

static abstract class SimpleController implements Controller {
    private String[] requiredParams;
    private String viewName;

    public void setRequiredParams(String[] requiredParams) {
        this.requiredParams = requiredParams;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }

    final public ModelAndView handleRequest(HttpServletRequest req,
                                            HttpServletResponse res) throws Exception {
        ...
    }


    public abstract void control(Map<String, String> params, Map<String, Object> model) throws Exception;
}

}

您需要學習 Spring 基礎知識。 您對哪些注釋做了不正確和不完整的理解。 以下鏈接提供了有關這些方面的良好知識。 Go 通過這些,修改你的代碼,你將解決這個問題而無需幫助。

Spring 框架注解

Spring 注釋 - JournalDev

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM