【问题标题】:Why does the <c:if> statement doesn't execute in the jsp? [duplicate]为什么 <c:if> 语句不在 jsp 中执行? [复制]
【发布时间】:2014-10-08 21:19:47
【问题描述】:

这是一个 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:if 标记的c:out 语句,那么它可以工作。但它错位了 UI 中的两个输入字段。在jsp中可以看到divmycontainer代码。我希望错误消息显示在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:outc:if 包裹的标签有效。

【问题讨论】:

  • @SotiriosDelimanolis 你能帮我解决这个问题吗,拜托。谢谢。

标签: html spring jsp spring-mvc jstl


【解决方案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 等设置了 Targeted Runtime ...

请参考这个问题here

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

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

然后使用如下形式:错误:

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

请参考以下教程,herehere

希望对你有帮助

【讨论】:

  • 你能回答这个问题吗?请。
  • 我正在回答这个问题! ,试着用我提供给你的解决方案来解决你的问题。
  • 检查我的更新答案
  • 谢谢。我确实尝试过使用 Spring 表单。但我需要创建一个新的表单类和验证器。所以我利用了modelAttribute本身。
  • 哦。对不起。我错过了我第一条评论中的 URL stackoverflow.com/questions/26201424/… 你能回答这个问题吗?请。
【解决方案2】:

我创建了一个名为 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:if 标签工作了。 但是,仍然在 jsp 中收到警告“找不到“http://java.sun.com/jsp/jstl/core”的标记库描述符。尝试增加动态 Web 模块项目方面的版本,因为当前 JSP 版本可能不支持这种引用方法( 1.1).",

【讨论】:

    猜你喜欢
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2019-11-06
    相关资源
    最近更新 更多