mvc:annotation-driven和@PathVariable的问题

时间:2014-08-22 18:18:10

标签: spring-mvc

不确定我在这里做错了什么,但是当我没有在我的servlet中定义mvc:annotation-driven时,@PathVariable返回的值没有显示在我的jsp页面中,当我定义时注释驱动,所有其他链接,例如home被破坏了,我得到了所请求的资源不允许使用指定的HTTP方法(不支持请求方法GET)。

    @Controller("HealthCheckController")
    @RequestMapping("/healthCheckSummary")

    public class HealthCheckController {

    private Log log = LogFactory.getLog(GolSimpleMappingExceptionResolver.class);
    private HealthCheckService healthCheckService = null;
    private IEappDataAccessFacade dataAccessFacade;

    @RequestMapping(value = "/{username}/{password}", method = RequestMethod.GET)
    public String getEappStatus(@PathVariable String username, @PathVariable String password, Model model){

        String dbConnectivityStatus = getDatabaseConnectivityStatus() ? "online" : "offline";

        if (!username.equals("lola") || !password.equals("123")) {
            // wrong credentials were provided
            log.error("The login credentials in the header are not valid." + username + password);
            throw new RuntimeException( "Unable to continue, the login credentials in the header are not valid." + username + password);
        }

        model.addAttribute("healthCheckSummary", dbConnectivityStatus); 

        return "healthCheckSummary";
    }

    public HealthCheckService getHealthCheckService()
    {
        return healthCheckService;
    }

    public boolean getDatabaseConnectivityStatus() {

        String result = “OK”;

        if (result != null) {
            return true;
        }
        return false;
    }
}

哦,在应用程序上下文中我们定义了

<tx:annotation-driven />

JSP页面

<%@ page language="java"%>
欢迎$ {username} - $ {密码}

Eapp目前为$ {healthCheckSummary}

1 个答案:

答案 0 :(得分:0)

两件事:

  1. 您永远不会将usernamepassword @PathVariable放入模型中。您的jsp页面无法知道它们甚至存在,因为在代码离开getEappStatus后您丢失了对它们的任何引用。将以下内容添加到处理程序方法中:

    model.addAttribute("username", username);
    model.addAttribute("password", password);
    

    看看是否有效。

  2. 您可能希望添加<mvc:annotation-driven/>,以避免在使用某些spring-mvc注释时出现意外情况。这不是绝对必要的,但是当某些@RequestBody不起作用时,可能会让您感到头疼。