How to open a partial view from controller as a Jquery Mobile dialog?

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

Was this solution helpful to you?

Just Added Q & A:

Find solution

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.