Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 建议使用Spring MVC显示错误消息,而不必求助于@ModelAttribute_Spring Mvc - Fatal编程技术网

Spring mvc 建议使用Spring MVC显示错误消息,而不必求助于@ModelAttribute

Spring mvc 建议使用Spring MVC显示错误消息,而不必求助于@ModelAttribute,spring-mvc,Spring Mvc,我在Spring MVC应用程序中有以下方法框架: @RequestMapping(value = "/activateMember/{token}", method = RequestMethod.GET, produces = "text/html") public String activateMember(@PathVariable("token") String token) { ... } 如果标记因某种原因无效,我试图显示一条错误消息。但是,我在方法参数中没有modeldattr

我在Spring MVC应用程序中有以下方法框架

@RequestMapping(value = "/activateMember/{token}", method = RequestMethod.GET, produces = "text/html")
public String activateMember(@PathVariable("token") String token) {
...
}
如果
标记因某种原因无效,我试图显示一条错误消息。但是,我在方法参数中没有
modeldattribute
,我真的不想要一个。当然,我不能使用
Errors
BindingResults
参数,因为缺少
ModelAttribute
及其相应的
表单

所以我的问题是:

鉴于上述方法签名,在不引入ModelAttribute的情况下,建议以什么方式显示错误消息

如果从该方法返回的
字符串是一个viewname(Spring默认值),则只需为这种情况创建一个视图,然后执行以下操作:

@RequestMapping()
public String activateMember(@PathVariable("token") String token) {
    if(checkToken(token)){
        doProcess();
        return "userprofile";
    } else {
        return "badtoken"
    }
}
在更复杂的情况下,您可能有一个异常层次结构,与坏令牌相关。(令牌已过期,令牌不正确等等)。您可以在同一控制器中注册:

@RequestMapping()
public String activateMember(@PathVariable("token") String token) {
    return activate(token); // This method may throw TokenException and subclasses.
}

@ExceptionHandler(TokenException.class)
public ModelAndView tokenException(TokenException e){
    // some code
    return new ModelAndView("badtoken", "exception", e);
}

如果从该方法返回的
字符串
是一个viewname(Spring默认值),则只需为这种情况创建一个视图,然后执行以下操作:

@RequestMapping()
public String activateMember(@PathVariable("token") String token) {
    if(checkToken(token)){
        doProcess();
        return "userprofile";
    } else {
        return "badtoken"
    }
}
在更复杂的情况下,您可能有一个异常层次结构,与坏令牌相关。(令牌已过期,令牌不正确等等)。您可以在同一控制器中注册:

@RequestMapping()
public String activateMember(@PathVariable("token") String token) {
    return activate(token); // This method may throw TokenException and subclasses.
}

@ExceptionHandler(TokenException.class)
public ModelAndView tokenException(TokenException e){
    // some code
    return new ModelAndView("badtoken", "exception", e);
}