Mutual transmission of jsp processing forms and JS and JAVA Variables

Source: Internet
Author: User

1: hyper-connection jump

 

During hyper-connection, we often connect to a jsp or Action, for example:

 

XXX

 

In this case, there will be a problem: in test. in jsp, except the param1 and param2 parameters you pass, you use request. if getParamter () is used to retrieve any form element of the original page, it will be null, because this method will generate a new request lifecycle. In this request, it will only contain the elements that follow after the superjoin? Param1, so I suggest that you do not use this hyper-connection method if it is not a simple page Jump (for example, a logon page.

 

 

 

2: Both passing parameters and submitting forms

 

If you want to pass parameters in the first way and submit a form, you can do the following:

 

Handle the following in the script:

 

FunctioncommonSubmit (url)

 

{

 

Form1.action = "/jsp/managerAction. do? "+ Url;

 

Form1.submit ();

 

}

 

Submitting to Action is the same as jsp, but remember that method = post cannot be less; otherwise, it only submits the form without passing the param parameter (opposite to the first one :))

 

 

 

3: the request in the Action will not be lost.

 

I wonder whether you have noticed this benefit. In the Struts architecture, an ActionServlet acts as the controller of MVC. After submitting a jsp page, the request is uploaded to the ActionServelt, the ActionServlet will call the corresponding Action method according to the configuration in the struts-config.xml, and pass the request obtained from the jsp to the Action class, so that the request lifecycle is continuous, that is, you submitted the form in jsp, executed the method in Action, and then returned to the jsp page, using request. getParamter () takes the form Element value of the jsp page and finds that it is still there. This is a good implementation method for selecting items such as the page drop-down list to prevent resetting.

 

 

 

4: When passing parameters, the parameter has spaces.

 

If you submit a form with parameters such as form1.action = "/jsp/Action. do? Param1 = "+ value1. Note that if value1 contains spaces

 

 

 

Request. getParameter ("param1 ");

 

 

 

Only the value before the space is obtained. Therefore, if a parameter with spaces is passed, it is recommended that it be converted to a specific string, value1 = value1.replaceAll ("", "% NULL % ");

 

Then return it in Action: request. getParamter ("param1"). replaceAll ("% NULL % ","");

 

 

 

5: form-data attributes

 

If you want to upload files, note that if the enctype = "multipart/form-data" attribute exists in a form, is not allowed to receive other form element types except type = file.

 

That is, if you put textfield in the same form and the form has enctype = "multipart/form-data", request. the value of getParamter text is null. The simplest solution to this problem is to put a form separately, and only submit the form when uploading the file.

 

 

 

6: Open Mode dialog box in jsp

 

Place a jsp page in a mode dialog box to open it. Before the mode dialog box disappears, the original jsp page cannot be operated. It is particularly suitable for the parent page to do some additional selection operations, and there is no need to jump to the new jsp page, the mode of opening the dialog box is as follows:

 

 

 

Functionopen (){

 

If (window. showModelDialog ())

 

{

 

Var returnValue = showModelDialog ("/jsp page path included in the mode window ";

 

Help = 0; status = 0; center = yes; dialogWidth = 100pt; dialogHeight = 100pt ");

 

}

 

 

 

}

 

 

 

7. Mutual transmission of js and java Variables

 

In jsp, js variables are often assigned to java variables, or java variables are assigned to js variables. Here we will summarize the general processing methods as follows:

 

 

 

It is easy to pass the java variable to js. var a = "<% = javaParam %>"; note that you must add the quotation marks;

 

 

 

Js variables are a little complex for java. Generally,

 

And then assign the js variable value to it in the script:

 

 

 

Var jsParamValue = 'aaa ';

 

Form1.jsParam. value = jsParamValue;

 

 

 

Then you can request. getPrameter ("jsPrama"); To Get The js variable value.

 

 

 

Supplement: ajax is actually a real-time technology that transfers JS variables to the server without refreshing the page.

 

 

 

Summary:

 

You cannot assign values to java variables on the page using JS variables;

 

Instead, you can use var jsParam = "<% = javaParam %>" to assign the java variable to the js variable.

 

From angus_17

Related Article

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.