JSTL,使用<choose>标签跳过条件

时间:2018-02-06 22:05:55

标签: jsp jstl jspx

我是JSTL和JSPX技术的新手。

我遇到一个问题,当我使用c:choose,c:when和c:否则标签加载基于servlet URL的东西时,它似乎忽略条件并打印所有内容。作为测试,这是我的jspx文件

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1" session="true"/>
<jsp:output doctype-root-element="html"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-> transitional.dtd"
        omit-xml-declaration="true" />
<html xmlns:c="http://java.sun.com/jsp/jstl/core">


<head>
    <title>dummy application</title>
</head>

<body>
<c:choose>
    <c:when test="${empty fixed}">
        This is empty
    </c:when>

    <c:otherwise>
        Dank Memes
    </c:otherwise>
</c:choose>

</body>
</html>
</jsp:root>

我的Servlet具有以下代码:

@WebServlet(urlPatterns = { "/Start", "/Start/*", "/StartUp", "/StartUp/*" })

public class Start extends HttpServlet {
private static final String FIXED = "fixed";

protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {
        Writer ro = response.getWriter();

        String path = request.getRequestURI();
        String last = path.substring(path.lastIndexOf('/') + 1);
        System.out.println(last);

        if(last.toString().contains("Fixed")) {
            ro.append("This url contains Fixed \n");
            request.setAttribute(FIXED, "blah");
//          ro.append(getServletContext().getAttribute(FIXED).toString());
            System.out.println("Contains fixed");
            System.out.println("Value contained within the FIXED variable is " + getServletContext().getAttribute(FIXED));
        }
        else {
            System.out.println("doesn't contain fixed");
            System.out.println("Value contained within the FIXED variable is " + getServletContext().getAttribute(FIXED));
        }
request.getRequestDispatcher(startPage).forward(request, response);
    }

当你刚进入/开始进入网址时,它应该打印出“这是空的”,当你传入/开始/固定时,它应该打印出“Dank memes”。然而,当任何一个网址输入时,打印出来的是“这是空的Dank memes”。它似乎完全跳过了选择标签。任何帮助解决这个问题将不胜感激。

0 个答案:

没有答案