Why does the <c:if> statement doesn't execute in the given jsp?
-
This is a Spring Web MVC project where I do input validation in server side. If there are any errors, then I add it to the model before sending it to the view. Controller @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="Oracle Technology Network for Java Developers> <head> <script src="Page on jquery.com"></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> The following piece of jstl code in jsp doesn't work <c:if test="${not empty invalidLength}"> <c:out value="Register Number should be 12 digits"/> </c:if> Also if I use the c:out statement without c:if tag, then it works. But it misaligns two input fields in UI. You can see the div mycontainer code in jsp. I want the error message to be shown below the regNo input field, but at the same time regNo and studetnName input field should be center aligned in a single line.
-
Answer:
You are only setting either of the attributes "nullValue" , "invalidLength" and "studentResult". This code will only set one attribute. 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"; } Then you are trying to access both nullValue and invalidLength which is not possible. Only the following code is getting executed on the basis of condition if(regNo.equals(null) || regNo.isEmpty()){ model.addAttribute("nullValue", "Register Number field cannot be empty"); return "header"; } Let me know if you need better insight.
Vivek Vermani at Quora Visit the source
Related Q & A:
- Why query optimizer doesn't use negative filter index?Best solution by Database Administrators
- Why doesn't ClickOnce in Visual Studio deploy content files from dependent assemblies?Best solution by Stack Overflow
- Why doesn't my PHP function work as expected?Best solution by Stack Overflow
- Why doesn't MySQL upload my data properly?Best solution by php-mysql-tutorial.com
- "Why am I unable to send out group im's with links, the link doesn't show up?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.