基于If条件在Spring控制器中获得不同的返回值

时间:2017-03-11 16:00:21

标签: java spring model-view-controller spring-boot

我想根据私有方法结果显示返回函数。如果private方法返回true,那么我想显示jsp页面。如果是false我想显示一些字符串作为结果。

@RequestMapping(value = "/accept", method = { RequestMethod.GET, RequestMethod.POST })
public String accept(HttpServletRequest request, Principal principal, Model model) throws Exception {
    if (checkMember(currentuser_id, token, principal, model) == true) {
        actionService.ProposalMemberRequestAccepted(username, request_id, reference_key);
        messageService.reject(username);
        return "Some jsp page.jsp";
    } else {
        System.out.println("Already Member");

        return "You are already a Member of this Project";
    }
}

private boolean checkMember(int currentuser_id, String token, Principal principal, Model model) throws Exception {
    String username = principal.getName();
    currentuser_id = comDao.getuser_id(username);
    token = messageService.accept(username);
    int uid = actionService.checkmember(currentuser_id, token);
    model.addAttribute("userid", uid);
    model.addAttribute("user_id", currentuser_id);
    if (currentuser_id != uid) {
        System.out.println("true");
        return true;
    } else {
        System.out.println("false");
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用:

public String accept(HttpServletRequest request, HttpServletResponse response, Principal principal,Model model) { //Get HttpServletResponse instance
    if(checkMember(currentuser_id, token, principal,model)) {
        return "somepage"; //Configure View Resolver for JSP
    } else {
        try {       
            PrintWriter out = response.getWriter();
            out.println("You are already a Member of this Project");
            out.close();
        } catch (IOException ex) { 
            //Use Logger
        }
        return null;
    }
}