为什么<c:if>语句不能在jsp中执行?</c:if>

时间:2014-10-08 21:19:47

标签: html spring jsp spring-mvc jstl

这是一个Spring Web MVC项目,我在服务器端输入验证。如果有任何错误,我会在将其发送到视图之前将其添加到模型中。

控制器

@Controller("resultController")
public class ResultController {

    private final ResultService resultService;

    @Autowired
    public ResultController(ResultService resultService) {
        this.resultService = resultService;
    }

    // @RequestMapping(value = "/search", method = RequestMethod.GET)
    @RequestMapping(value ="/template", method = RequestMethod.GET)
    public String getPersonList(ModelMap model) {
        System.out.println("We are coming into this place");
        return   "header";
    }


    @RequestMapping(value = "/number", method = RequestMethod.POST, params = { "regNo" })
    public String getStudentResult(@RequestParam(value = "regNo", required = true) String regNo, ModelMap model){

        //Server side validation
        if(regNo.equals(null) || regNo.isEmpty()){
            model.addAttribute("nullValue", "Register Number field cannot be empty");
            return "header";
        }else if(regNo.length() != 12 ){
            System.out.println("This Sys out is shown");
            model.addAttribute("invalidLength", new String("invalid"));

            return "header";
        }else{

            model.addAttribute("studentResult",resultService.getStudentResult(regNo));      
            return "numberResult";      
        }
    }   
}

header.jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<head>
 <script src="http://code.jquery.com/jquery.min.js"></script>
     <style>
        #mycontainer, h1, h3 {
            text-align:center;
        }
        form {
            display:inline-block;
        }       
    /*  #regNoErrorMsgNumber {
            display: none;
            background: brown; 
            color: white;
        } */
    </style>

</head>
<body>

<div id="mycontainer">  
    <form method="post" action="number" id="number">
    <!--    <div id="regNoErrorMsgNumber">Only numbers are allowed</div> -->
            <div style="text-align: center;" >
                    <!-- //TODO: Only number, no spaces, no special symbol and 12 digit check-->                

                         <input  width="20" type="text" data-validation="numbers" id="regNo" name="regNo" size="30" maxLength="50" placeholder="Enter Register Number"> <b>OR</b>       
                        <div> 
                            <c:if test="${not empty nullValue}">
                                <c:out value="${nullValue}"/>
                            </c:if>


                            <c:if test="${not empty invalidLength}"> 
                                <c:out value="Register Number should be 12 digits"/>
                            </c:if>                      
                        </div>                  
            </div>      
    </form>           

    <form method="post" action="name" id="name"> 

                <input  type="text" id="studentName" name="studentName" size="30" maxLength="50" placeholder="Enter Student Name"></input>

    </form>                             
</div>             
            <div style="text-align: center;">
                <input id="inputFields" type="button" value="Search"  />
             </div>

    <!-- </form> -->
<script>    
    $(document).ready(function(){
        $('#inputFields').click(function(event){
            if (document.getElementById('regNo').value !=""){           

                $("#number").submit();

            }else if(document.getElementById('studentName').value !=""){
                $("#name").submit();
            }
        });
    });    
</script>

</body>

jsp中的以下jstl代码不起作用

<c:if test="${not empty invalidLength}"> 
    <c:out value="Register Number should be 12 digits"/>
</c:if>  

此外,如果我使用不带c:out标记的c:if语句,那么它可以正常工作。但它在UI中错位了两个输入字段。您可以在jsp中看到div mycontainer代码。我希望错误消息显示在regNo输入字段下方,但同时regNostudetnName输入字段应在一行中居中对齐。

PS:我得到Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core". Try increasing the version of the Dynamic Web Module project facet, as this method of reference may not be supported by the current JSP version (1.1).,但c:out标记被c:if包裹起作用。

2 个答案:

答案 0 :(得分:1)

请尝试以下方法:

  • 如果您正在使用maven,请将此添加到您的依赖项中,maven将为您添加jar:

    <dependencies>
    
              <dependency>
                     <groupId>jstl</groupId>
                     <artifactId>jstl</artifactId>
                     <version>1.2</version>
               </dependency>
    
    </dependencies>
    
    • 如果您不使用maven,请将jstl库添加到您的项目中(jstl-1.2.jar)

    • 确保为项目,Tomcat,Glassfish等设置目标运行时...

请参阅此问题here

对于错误部分,请使用spring form标签中的<form:errors>: - 首先将以下内容添加到您的页面:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

然后使用表单:如下所示的错误:

<form:errors path="userName" cssClass="error" element="div" />

请参阅以下教程,herehere

希望有所帮助

答案 1 :(得分:0)

我创建了一个名为ProjectErrors的新类,我按如下方式更改了代码

if(regNo.equals(null) || regNo.isEmpty()){
        univErrors.setHasError(true);
        model.addAttribute("isNull", univErrors  );             
    }else if(Pattern.matches("[a-zA-Z]+", regNo)){
        univErrors.setHasError(true);
        model.addAttribute("onlyNumbers", univErrors  );    
    }else if(regNo.length() != 12 ){
        univErrors.setHasError(true);
        model.addAttribute("length", univErrors  );                 
    }

我改变了像这样的jsp

 <c:if test="${length.hasError}"> 
                        <c:out value="Register Number should be 12 digits."/>
                    </c:if>  

                    <c:if test="${onlyNumbers.hasError}">
                        <c:out value="Register number can contain only digits."/>
                    </c:if> 

我的错误类看起来像这样

public class ProjectErrors {

    public Boolean hasError;

    public ProjectErrors(boolean b) {
        // TODO Auto-generated constructor stub
        hasError = b;
    }

    public Boolean getHasError() {
        return hasError;
    }

    public void setHasError(Boolean hasError) {
        this.hasError = hasError;
    }


}   

现在我看到c:如果标签正常工作。 但是,仍然在jsp&#34中得到警告;找不到&#34; http://java.sun.com/jsp/jstl/core&#34;的标签库描述符。尝试增加Dynamic Web Module项目方面的版本,因为当前的JSP版本(1.1)可能不支持此引用方法。&#34;,