0

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="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>

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.

PS: I get 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)., but c:out tag with being wrapped with c:if works.

vjy
  • 1,184
  • 1
  • 10
  • 24
sofs1
  • 3,834
  • 11
  • 51
  • 89

2 Answers2

1

please try the following :

  • if you are using maven , add this to your dependencies and maven will add the jar for you :

    <dependencies>
    
              <dependency>
                     <groupId>jstl</groupId>
                     <artifactId>jstl</artifactId>
                     <version>1.2</version>
               </dependency>
    
    </dependencies>
    
    • if you are not using maven add the jstl library to your project (jstl-1.2.jar)

    • make sure you set a Targeted Runtime for your project , Tomcat , Glassfish , etc ...

and please refer to this question here .

for the errors part , use the <form:errors> from spring form tags : - first add the following to your page :

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

then use the form:errors like the following :

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

please refer to the following tutorials , here and here .

Hope that helps

Community
  • 1
  • 1
  • Could you answer this question? Please. – sofs1 Oct 09 '14 at 07:02
  • i am answering this question !! , try to solve your problem , with the solutions i have provided to you. –  Oct 09 '14 at 07:03
  • Thanks. I do tried using Spring form. But I need to create a new form class and validator. So I made use of modelAttribute itself. – sofs1 Oct 09 '14 at 07:13
  • Oh. Sorry. I missed the URL in my first comment http://stackoverflow.com/questions/26201424/why-do-i-get-two-different-project-context-for-my-spring-mvc-web-application?noredirect=1#comment41138381_26201424 Could you answer this question? Please. – sofs1 Oct 09 '14 at 07:14
  • accpet the answer if it solves your problem –  Oct 09 '14 at 07:19
  • I can accept my own answer only after 2 days it seems. – sofs1 Oct 09 '14 at 07:26
0

I created a new class called ProjectErrors and I changed the code as follows

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  );                 
    }

I changed the jsp like this

 <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> 

And my error class looks like this

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;
    }


}   

Now I see c:if tag working. But, still get the warning in jsp "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).",

sofs1
  • 3,834
  • 11
  • 51
  • 89