這是一個Spring Web MVC項目,我在服務器端進行輸入驗證。如果有任何錯誤,那麼在將它發送到視圖之前將其添加到模型中。爲什麼<c:if>語句不能在jsp中執行?
控制器
@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
輸入字段下方顯示錯誤消息,但同時regNo
和studetnName
輸入字段應該在一行中居中對齊。
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
包裝作品。
@SotiriosDelimanolis你能幫我解決這個問題,請。謝謝。 – user3705478 2014-10-08 21:24:28
你使用maven嗎? – 2014-10-09 05:49:57