gpt4 book ai didi

java - Spring MVC,JSP - 所需的 boolean 参数 'guest' 不存在

转载 作者:行者123 更新时间:2023-11-29 07:36:46 25 4
gpt4 key购买 nike

我正在尝试使用 guest 和用户的权限做一些事情,我做了一些事情,但它不起作用,出现标题中的错误。

/home 和/logged/home 使用相同的 jsp

我希望“注册”和“登录”不会为已登录的用户显示

主 Controller :

@Controller  
public class MainController {

DBConnection db = new DBConnection();

@RequestMapping(value = "/home.html", method = RequestMethod.GET)
public ModelAndView HomePage(@RequestParam boolean guest) {

User user = new User();
user.setGuest(guest);
user.isGuest();

ModelAndView main = new ModelAndView("Main");

return main;

}

@RequestMapping(value = "/logged/home.html", method = RequestMethod.GET)
public ModelAndView homePageforUsers(@RequestParam boolean loggeduser) {

User user = new User();
user.setUser(loggeduser);
user.isUser();

ModelAndView main = new ModelAndView("Main");

return main;

}

主.jsp:

<c:if test="${user.isUser()}">
<div class="cart">
<a href="http://localhost:8080/OnlineShop/cart.html"
style="text-decoration: none; color: #000"> Cart </a>
<div class="logout">
<a href="http://localhost:8080/OnlineShop/home.html"
style="text-decoration: none; color: #000"> Log out </a>
</div>
</div>
</c:if>

<c:if test ="${user.isGuest()}">
<div class="register">
<a href="http://localhost:8080/OnlineShop/registration.html"
style="text-decoration: none; color: #000"> Register </a>
</div>
<div class="login">
<a href="http://localhost:8080/OnlineShop/login.html"
style="text-decoration: none; color: #000"> Log in </a>
</div>
</c:if>

最佳答案

<a href="http://localhost:8080/OnlineShop/home.html"

这部分会触发对您的 HomePage Controller 的调用,并且其中没有可用的 guest 查询参数,因此 Spring MVC 会提示缺少该必需参数。

默认情况下,使用 RequestParam 注释的参数是必需的。因此,您要么应该将它们设为可选,例如:

@RequestParam(required=false) boolean guest

或者为它们提供一些合理的默认值,如下所示:

@RequestParam(defaultValue="false") boolean guest

如果您不能使用建议的解决方案,请在每个请求中提供这些强制性参数,在您的情况下是这样的:

<a href="http://localhost:8080/OnlineShop/home.html?guest=false"
style="text-decoration: none; color: #000"> Log out </a>

关于java - Spring MVC,JSP - 所需的 boolean 参数 'guest' 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34706632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com