Java EE----Action Accept request parameters

Source: Internet
Author: User
Tags dateformat

First, how to accept the request parameter in action parameter
* It is not recommended to use the servlet API in action in actual development


1, three kinds of ways:
1) automatically receive request parameters via action member variable
Define a member variable in action, the variable name and the form element Name property are consistent, and a setter is provided for the member variable

<interceptor name= "params" class= "Com.opensymphony.xwork2.interceptor.ParametersInterceptor"/> Complete parameter encapsulation

2) Use a separate model object to complete the encapsulation based on the client OGNL parameter name
Create a model member variable in action
Page user name <input type= "text" name= "User.username"/> <br/>

Note: The Get and set methods are provided for the model object in the action.

3) Implement Modeldriven interface to complete the parameter encapsulation
The STRUTS2 provides an Modeldriven interface that specifies which member of the Action is the model

public class LoginAction4 extends Actionsupport implements Modeldriven<user>
Public User Getmodel () {
return user;
}
Note: Model objects must be instantiated manually

Mode two and way three in the use of the essential difference: the way two can define multiple model, the way three can only define a model object in one action

<interceptor name= "Modeldriven" class= "Com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/> Provides a separate interceptor call Modeldriven implementation


2. Collection Object type data encapsulates List, MAP


1) List Package
Action private list<user> users;
JSP <input type= "text" name= "Users[0].username"/>

2) Map Package
Action private map<string, user> UserMap;
JSP <input type= "text" name= "usermap[' one '].username"/>

3. Custom type Converters
STRUTS2 provides many types of converters internally, but when a user submits a data type format that is not provided internally by the Struts2-------------requires a custom type converter

TypeConverter defines public object Convertvalue (Map context, Object target, Member Member, String PropertyName, Object value, Class ToType);
Subclass Defaulttypeconverter Definition public Object Convertvalue (Map context, object value, Class totype)
* Context Ognl Contextual object, value to convert values, ToType conversion target type
Subclass Strutstypeconverter Splits the Convertvalue (Map context, Object value, Class ToType) into two methods
* ConvertFromString (Map context, string[] values, Class toclass) convert String to complex object type in action
* ConvertToString (Map context, object O) Converts the object type copied in action to type String

Custom converters typically inherit Defaulttypeconverter or Strutstypeconverter

Write converters
public Object Convertvalue (Map context, object value, Class totype) {
This method has two levels of meaning
First layer, support string conversion complex type
Second layer, support for complex type conversion string
DateFormat DateFormat = new SimpleDateFormat ("YyyyMMdd");
if (ToType = = String.class) {
Converting a complex object to a String type
Date date = (date) value;
return Dateformat.format (date);
} else if (ToType = = Date.class) {
Converting a string to a complex data type
Value is an array of type string
String strdate = ((string[]) value) [0];
try {
Return Dateformat.parse (strdate);
} catch (ParseException e) {
E.printstacktrace ();
}
}
Return Super.convertvalue (context, value, ToType);
}

To configure the converter:
1) Local configuration, configuration action contains a special attribute type, requires the converter actionclassname-conversion.properties (put in the action of the package)----for the properties of the action
Configuration Format: Property name = Converter Buytime=cn.itcast.struts2.demo3.mydateconvertor

* Local configuration is valid only for the current action


2) global configuration, so that all java.util.Date types in the system use Cn.itcast.struts2.demo3.MyDateConvertor configuration xwork-conversion.properties (put to the SRC root directory) ---for type
Configuration format: type = Converter Java.util.Date = Cn.itcast.struts2.demo3.MyDateConvertor

4. type conversion error handling
If an error occurs in the type conversion, the server side calls Addfielderror to add an error message that automatically jumps to the logical view input

<interceptor name= "Conversionerror" class= "Org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/ > Responsible for handling type conversion errors

1) After an error, configure result name= "input" in <action> to jump to the submission page
2) on the Submit page, display the error message
<!--introducing Struts Tag Library--
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<!--display error messages--
<s:fielderror></s:fielderror>

3) Create the class name in the package where the action class is located. Properties such as Cn.itcast.struts2.demo3.ProductAction under CN.ITCAST.STRUTS2.DEMO3 package creation Productaction.properties
Information configuration format: Invalid.fieldvalue. property name = Error message

Java EE----Action Accept request parameters

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.