Struts exercise-form submission, struts exercise form

Source: Internet
Author: User
Tags i18n

Struts exercise-form submission, struts exercise form

Exercise to prevent repeated submission of forms:

Before performing struts exercises, you must first prepare for creating a web project, import the jar package and configure web. xml.

I usually import the following jar packages at one time (it may not be necessary to do so many exercises with one knowledge point)

Download the above jar

Filter needs to be added to web. xml:

The configuration is as follows:

After completing the preceding configuration, create struts. xml under src (of course not required for the time being )!

Now we can do your job!

The following example shows my exercises to prevent repeated submission of forms:

1. Request sending page:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

2. Successful submission page:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

3. Submit the job repeatedly. The error page is displayed:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

4. Action Code:

Package cn. itcast. action; import com. opensymphony. xwork2.ActionSupport; public class TokenAction extends ActionSupport {@ Overridepublic String execute () throws Exception {System. out. println ("user registration... "); return SUCCESS ;}}

5. struts configuration information:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <! -- Configure struts --> <struts> <! -- Constant configuration --> <! -- Configure the default consumer set for the web Application --> <constant name = "struts. i18n. encoding" value = "UTF-8"> </constant> <! -- When the value is set to true, when the struts file is changed, the system automatically reloads the file --> <constant name = "struts. configuration. xml. reload "value =" true "> </constant> <! -- Apply the development mode of struts2. If the value is true, more detailed error information can be printed --> <constant name = "struts. devMode "value =" true "> </constant> <! -- Specify the international resource file required by struts --> <constant name = "struts. custom. i18n. resources" value = "tokenerror"> </constant> <! -- Package configuration --> <! -- The package name is default and inherits struts-default --> <! -- The struts2 framework uses packages to manage actions and interceptors. Each package is a collection of multiple actions, multiple interceptors, and multiple interceptor references. You can use a package to divide a group of logically related components, such as Action, Result, and Interceptor into one group. A package is a bit like an object, and can inherit other packages or be inherited by other packages, you can even define an abstract package --> <package name = "default" extends = "struts-default"> <! -- Add action: submit the form repeatedly --> <action name = "token" class = "cn. itcast. action. TokenAction"> <! -- Configuration result page, with name = "success" omitted --> <result>/index. jsp </result> <result name = "invalid. token ">/token_error.jsp </result> <! -- Redefinition interceptor --> <interceptor-ref name = "defaultStack"> </interceptor-ref> <interceptor-ref name = "token"> </interceptor-ref>/ action> </package> </struts>

6. web. xml configuration information:

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name> </display-name> <! -- Set the filter --> <filter> <! -- Filter name --> <filter-name> struts </filter-name> <! -- The implementation class of the filter, responsible for specific filtering transactions --> <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <! -- Set the filter ing --> <filter-mapping> <! -- Filter name --> <filter-name> struts </filter-name> <! -- The URL that the filter filters --> <url-pattern>/* </url-pattern> </filter-mapping> <! -- Set the list of welcome files for the web site --> <welcome-file-list> <! -- Specify the welcome file name --> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>

7. tokenerror. properties file:

Click Add to notify the customer of the error message in Chinese!



Package and download. It will be useful to you!


Struts2 form submission

<Form action = "login. action" method = "post">
Name: <input type = "text" name = "username"> <br>
Password: <input type = "text" name = "password"> <br>
<Input type = "submit" value = "click to log on">

</Form>
If the form is written incorrectly, it means that the form does not respond when the from button is clicked. If the submit is written incorrectly, it means that the form is wrong, and then the browser will die. The latter is unlikely.

Form submission in struts2

After you click Export, js modifies the form action. If the page is not refreshed, the form action will not be automatically reset to sendSingleQuery. action.
A simple method is to add a line of js Code at the end of the doReport () method:
Form. action = "sendSingleQuery. action ";
Reset the value to the default action after export. The minimum code modification is required.

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.