Some error-prone problems when using struts to process web programs

Source: Internet
Author: User

1. Question about JSP page forms and <div> </div>.

Previously, there was a page in the project, because there were a lot of things on the page, so multiple <div> </div> were used,
Then, when I submit a form, the script called cannot find the form I submitted.
The reason is that <div> </div> indicates a region. When a form spans one or more Divs, a problem occurs,
The from form must be in the div.

Previously, there was a page in the project, because there were a lot of things on the page, so multiple <div> </div> were used,
Then, when I submit a form, the script called cannot find the form I submitted.
The reason is that <div> </div> indicates a region. When a form spans one or more Divs, a problem occurs,
The from form must be in the div.

For example:
 
<HTML: Form Action = "/infomanagement" target = "hiddenframe" method = "Post" enctype = "multipart/form-Data" style = "margin: 0px;">
<Div id = "employeeinfo" style = "display: none">
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0" style = "padding-left: 8px; padding-Right: 8px">
<Tr>
<TD nowrap> ...... </TD>
</Tr>
</Table>
</Div>

//------------------------------

<Div id = "centercontent" style = "display: none">
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" style = "padding-left: 8px; padding-Right: 8px">
<Tr>
<TD> ...... </TD>
</Tr>
</Table>
</Div>
</Html: Form>

In this case, the form spans multiple Divs. When submitting the form, the page reports an error: the form is not defined.

Solution 1: combine two divs into one. If you need to display the page effect, you can control the style of the two tables and place the form inside the div.
Solution 2: Check the submitted content. If you only need the content in one Div, you can put the form in only one Div,
For example:

<Div id = "employeeinfo" style = "display: none">
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0" style = "padding-left: 8px; padding-Right: 8px">
<Tr>
<TD nowrap> ...... </TD>
</Tr>
</Table>
</Div>

//------------------------------

<Div id = "centercontent" style = "display: none">
<HTML: Form Action = "/infomanagement" target = "hiddenframe" method = "Post" enctype = "multipart/form-Data" style = "margin: 0px;">
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" style = "padding-left: 8px; padding-Right: 8px">
<Tr>
<TD> ...... </TD>
</Tr>
</Table>
</Html: Form>
</Div>

2. About how to use struts to process the form with uploaded files

When processing a form with Struts, you must first add enctype = "multipart/form-Data" to the form, for example
<HTML: Form Action = "/infomanagement" target = "hiddenframe" method = "Post" enctype = "multipart/form-Data" style = "margin: 0px;">
.......
</Html: Form>
In addition, define the corresponding formfile object and (get/set method) in the corresponding * form. java)

Note the following:
<Table>
<Tr>
<TD>
<Logic: notempty name = "infoform" property = "values (employee). Values (photo)">
<Bean: Define id = "photo" name = "infoform" property = "values (employee). Values (photo)" type = "Java. Lang. String"/>
<Bean: Define id = "photonum" name = "infoform" property = "values (employee). Values (photonumber)" type = "Java. Lang. String"/>
<% --
Document. Write ("photo =" + <% = photo %> );
Document. Write ("src =" + <% = request. getcontextpath () + photo %> );
<HTML: hidden name = "infoform" property = "photonumber" value = "<% = photo %>"/>
-- %>
<HTML: hidden name = "infoform" property = "values (photolastname)" value = "<% = photonum %>"/>
<Tr>
<TD Height = "20" nowrap> <Div align = "Left"> <strong> <Bean: Message key = "STF. infocenter. photo "bundle =" staffing "/>:</strong> </div> </TD>
</Tr>
<Tr>
<TD nowrap>
<Div id = "img0" style = "display: Block"> "width =" 250 "/> </div>
<Div id = "img1" style = "display: none"> </div>
</TD>
</Tr>
<Tr>
<TD align = "center">
<Button class = "button" onclick = "fntoggle1 (this, 'img0 ', 'img1',' <Bean: Message key =" STF. basicinfo. normalsize "bundle =" staffing "/> ',' <Bean: Message key =" STF. basicinfo. scalesize "bundle =" staffing "/> ');"> <Bean: Message key = "STF. basicinfo. scalesize "bundle =" staffing "/> </button>
</TD>
</Tr>
</Logic: notempty>
</TD>
</Tr>
</Table>

In the above Code, pay attention to this line
[1]: <HTML: hidden name = "infoform" property = "values (photolastname)" value = "<% = photonum %>"/>

You can also write it like this.
[2]: <HTML: hidden name = "infoform" property = "photolastname" value = "<% = photonum %>"/>

[Note: The values here is only a map defined in form to facilitate the value transfer in pages and actions]

However, the Code contains the string file and enctype = "multipart/form-Data", which makes struts significantly different when assembling the form.
For example, my form name is informationform infoform = (informationform) form. I will directly describe it using infoform later.
If method [1] is used for form submission, the submitted information is stored in infoform [(hashmapz) Values,

Parameter Extraction Method:
String STR = (string) infoform. getvalue ("photolastname ");
If (STR! = NULL & Str. Trim (). Length ()> 0 ){
Infoform. setvalue ("photo", STR );
}


Method [2] is used for form submission. After submission, the information is stored in
Infoform [(commonsmultipartrequesthandle) multipartrequesthandle [elementsall and elementstext.

All colleagues who are recommended to work as Struts web projects should try their best to select the first method. Also, do not add enctype = "multipart/form-Data" to the pages that do not need to upload attachments ",
To avoid other problems, debugging takes time.

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.