Jsp C labels are generally used and js receives objects and object numbers in servlet, jsservlet

Source: Internet
Author: User

Jsp C labels are generally used and js receives objects and object numbers in servlet, jsservlet

 Common Use of jsp C labels and js receiving objects and object arrays in servlet

 

Because the popular javaWeb framework advocates frontend and backend separation, for example, servlet is rarely written in SpringMvc. Currently, most front-end JSPs are pure html and js, jstl is rarely used. The backend only processes frontend post, get requests, or page jumps without the need for tedious xml Path ing and filter filtering.

However, some items in the servlet scope, such as a fixed value and context path, are sometimes used ~, However, most of these things are limited to values rather than set values (or objects), so sun gives a very simple Value Method: C tag!

The following describes how to use the C tag. I am personally engaged in the content of Java Web open, and do not do many extensions =>

Before use, the page namespace must firstDeclare the C label and the basic declaration of jspThis is the most basic. By the way, if the jar package is referenced, it is also declared here, as shown below =>

Use$ {Key}You can obtain the passed value (here is the Float value of stockPrice). This key can be of any java type (for example, commonly usedList, map, String, Date, Object... And so on) =>

 

The preceding is a single value.Object TypeOrMap type, You need to click the access value, for example, the background is like this:

In jsp, the value is the same as the value in the object in js, like this ==>

 

 

When the value in stockPrice2 is a List, it is accessed in the same way as the list Value in js. It is enclosed in brackets =>

The preceding methods are available:[Array subscript], ["array subscript"],. get (subscript)It must be noted that the subscript cannot be caused by single quotes. In the first case, if the length of the retrieved array is dynamic, you need to add a layer of judgment to these methods, ~ This method is very undesirable and needs to be used at this timeForEach=>

 

 

When using forEach, you need to use the forEach tag. Even if the array is empty, no null value needs to be determined. It must be noted that the traversal object is placed in items, each sub-object in each object replaces the entire object of the sub-object with the value in var.VarStatusThis variable is the state of every variable stored, and usually uses the subscript where the object is located (the index variable name must be used and the variable name is fixed, if you need to filter a value in an object, you need to use the if tag. This tag can be nested in a loop or used independently ~ =>

Hmm ~, If ~ Else ~ In the C tag, this condition must be written in the choose tag, which may be troublesome. here only the code sample =>

<Select class = "btn col-sm-12" style = "font-weight: bold; color: black; pointer-events: stroke; opacity: initial;"> <c: if test = "$ {empty main. scoreC} "> <option value =" "> (null) </option> </c: if> <c: forEach items = "$ {scores}" var = "score"> <c: choose> <c: when test = "$ {main. scoreC eq score} "> <option value =" $ {score} "selected =" selected ">$ {score} </option> </c: when> <c: otherwise> <option value = "$ {score}" >$ {score} </option> </c: otherwise> </c: choose> </c: forEach> </select>

For example, to judge whether each condition needs to be written in the c: when tag (multiple tags may exist). If none of the conditions are met, the content in the c: otherwise tag is displayed, test the sample effect on your own, so I won't bother here (o ^. ^ o )~

Aha ~, The above are some of the common C labels that can be understood ~ Sorry, I missed one (* ^ *). This is Special: Most people will use the condition to determine whether it is null or not, and whether it is equal to a value:

For the former, the condition is generally written like this =>$ {EmptyVariable name}. This empty is a keyword in jsp. The variable name must be avoided ~ W (000000000000) w

For the latter, there are generally two writing methods: =>{ {variable nameEqVariable name B} or $ {variable name=Variable name B}. The latter generally determines numbers or basic types. The former generally determines objects. Don't forget, this "eq" is also the keyword o ~

 

Finally, this is of course the final Haha ( ̄)/. I also studied it for a while. Of course, it's not disgusting.(''Command ')..., Abstract: Generally, jsp cannot process the logic or troublesome logic. You need to put the List or Map in the servlet scope in js for processing. In this case, you may face a problem, how can you put the entire object in the js variable? Here you can confidently Answer you: UseFastJsonConvert string ~ Isn't it suddenly clear? haha, leave a brick first =>

1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>3 <%@ taglib uri="/WEB-INF/tlds/hs.tld" prefix="hs"%>4 <%@ page import="com.alibaba.fastjson.JSON" %>
1 <script type = "text/javascript"> 2 <% Object ob = request. getAttribute ("bonusMatrix"); %> 3 // initialize data 4 var bonusMatrixs = []; 5 function init (data) {6 bonusMatrixs = data; 7} 8 init (<% out. println (JSON. toJSONString (ob); %>); 9 </script>

And ~, You can directly use the json string to convert JavaScript objects ~, Is it easy to say that round (^ o ^) round

Ah ~, Wow ~ It's time to go to bed ~

 

 

// See the children's shoes here and hope they will☛Love technology, care for sister paper, care for single dogs, stay away from overtime, and stay away from changes in demand

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.