jstl

Spring: Test JSP Output in JUnit Test

隐身守侯 提交于 2020-01-03 10:56:10
问题 We have a API, which returns the JSP as the view, for example: @RequestMapping(value = "/cricket/{matchId}", method = RequestMethod.GET) public String getCricketWebView(HttpServletRequest request, @PathVariable("matchId") Integer matchId, ModelMap mv){ try{ return "webforms/cricket"; }catch(Exception e){ e.printStackTrace(); } return ""; } I wrote a unit test to test this out as follows: @Test public void test_cricket() { try { MvcResult result =this.mockMvc.perform(get(BASE + "/cricket/123")

JSTL sql:query variable

你说的曾经没有我的故事 提交于 2020-01-03 09:57:46
问题 I wrote the following code in a JSP file: <c:catch var="e"> <% int accountNumber = Integer.parseInt(request.getParameter("accountNumber")); int depositAmount = Integer.parseInt(request.getParameter("depositAmount")); %> <sql:query var='account' dataSource="jdbc/bank"> select * from account where AccountNumber=<%= accountNumber %> </sql:query> <c:choose> <c:when test="${account.first() == false}"> <p>Account not found</p> </c:when> <c:otherwise> <h3>Deposit Made</h3> <p>Account number: <%=

JSTL LOOP - counter +=3 Increment

半城伤御伤魂 提交于 2020-01-03 06:41:10
问题 I have to iterate a collections in JSTL, but I would like the index to be incremented +3 each loop, something like for (int i=0; i<50; i+=3) { } but in JSTL 回答1: use <c:forEach/> tag which exist as an alternative for while , do-while and for Loop in jstl via scriptlet <c:forEach var="i" begin="0" end="50" step="3" > <c:out value="${i}"/> </c:forEach> begin for initialization, end for termination and step for increment 回答2: `<c:forEach items="<object>" begin="<int>" end="<int>" step="<int>"

How to access JSTL tag from JavaScript

你说的曾经没有我的故事 提交于 2020-01-03 02:28:52
问题 Is it allowed to call JSTL tag inside JavaScript file like the following example : <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> function displayErrors() { alert("<form:errors path='*'>"); } If allowed, how to make it work correctly? 回答1: This is JSP <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <script> function displayErrors() { alert("<form:errors path='*'>"); } </script> You can use JSTL tags inside JSP file, not JS file. 来源: https:/

jsp:useBean call a specific method in a class

六眼飞鱼酱① 提交于 2020-01-03 00:02:57
问题 How can I call a specific method in a class using this tag? <jsp:useBean id="user" scope="??" class="com.example.User" type="com.example.User" /> 回答1: Assuming your bean User has a method called getName() <jsp:useBean id="user" scope="request" class="com.example.User" /> // ... <h1>Hello <jsp:getProperty name="user" property="name" /></h1> The scope could be something else than request : depends on what you want (session, page, etc) EDIT: your second question was about calling a business

jsp:useBean call a specific method in a class

谁说胖子不能爱 提交于 2020-01-03 00:02:53
问题 How can I call a specific method in a class using this tag? <jsp:useBean id="user" scope="??" class="com.example.User" type="com.example.User" /> 回答1: Assuming your bean User has a method called getName() <jsp:useBean id="user" scope="request" class="com.example.User" /> // ... <h1>Hello <jsp:getProperty name="user" property="name" /></h1> The scope could be something else than request : depends on what you want (session, page, etc) EDIT: your second question was about calling a business

i18n translation in JSP custom tag

旧街凉风 提交于 2020-01-02 06:57:09
问题 Is it possible to write a custom JSP tag to take an i18n message key and output the translation phrase for the given request? Normally in JSP/JSTL, I do: <fmt:message key="${messageKey}"><fmt:param>arg1</fmt:param></fmt:message> And I get the translation phrase. Now I need to do the following (there's a good reason for this): <custom:translate key="${messageKey}" arg="arg1"/> But I don't know how to look up the translation in the custom tag code. The TagSupport base class provides a

How to write if else condition using ternary operator in jstl

自作多情 提交于 2020-01-02 05:05:32
问题 Hi guys i want to write a if else condition using ternary in JSTL. i did it using jsp. My code is using jsp : <%= relAttributeValue != "false" && !relAttributeValue.equals("false") ? "rel=\"nofollow\"" : "" %> how can i achieve it using jstl 回答1: You mean Expression Language, EL in short, since that's the component that allows you use ${something} expressions, while JSTL is a tag library which gives you tag components like <c:set> . In EL, you can do it like this: <c:set var="ternaryResult"

How to redirect to error page when exception occurs from servlet?

风格不统一 提交于 2020-01-02 04:35:08
问题 I am writing a servlet, in that if any exception occurs i donэt want to display exception/error message on browser, so I will redirect to my customized error page. So I have done like this: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ //Here is all code stuff }catch(Exception e){ request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response); e1.printStackTrace(); } Is this the correct way, if I am wrong

JSTL message: Don't know how to iterate over supplied “items” with forEach

对着背影说爱祢 提交于 2020-01-02 02:18:09
问题 I am passing a List to <c:forEach> , yet I get the error stating that it doesn't know how to iterate over it. @RequestMapping("/viewall") public String viewAll(Model model) { // productService.findAllProducts() returns List<Product> model.addAttribute("everything", productService.findAllProducts()); // Also tried using iterator, but I get same error //model.addAtrribute("everything", productService.findAllProducts().iterator()); .... } The jsp page: <c:forEach items="${everything}" var="prod"