Struts2 Study Notes

Source: Internet
Author: User
Tags control label

Tag: struts2

Struts1 + webwork core = struts2

Steps for using struts2: 1. Import the JAR File
2. Add a configuration filter in Web. XML to intercept all requests/*, strutsprepareandexecutefilter
3. Add a struts. XML, package-action-result under SRC.
4. To implement control, you need to add the action class
4. 1. Implement the Action Interface
. Inherit the actionsupport class
. Directly create a class, but the method string execute () must be available ()
5. Modify the action tag in struts. xml and add an attribute class to the action class.


Define constants in struts. xml <constant name = "" value = "/>
Development Mode: struts. devmode = true/false
Default encoding: struts. i18n. Encoding = UTF-8 (default)

How to bind the value of a form element: 1. Use attribute: add an attribute with the same name to the action and provide the get/Set Method
2. Use Javabean: add an object to aciton and provide the get/set method.
The name of the form element, which should be the object name. attribute name
3. Use modeldriven

How to process multiple requests in an action Class 1. In this action class, add a method for each request
2. Add the corresponding configuration in struts. xml:
2. 1. First: add the attribute method to the action tag and specify the method.
2. 2. For each module, write a configuration file separately and include the file in struts. xml.
2. 3. 3. Dynamic Method: Add "! Method Name, for example, "house! Add"
2. 4. Fourth: wildcard:
<Action name = "House _ *" class = "com. houseaction" method = "{1}">


Default action: <default-action-ref name = "Index"/>
<Action name = "Index"> <result>/index. jsp </result> </Action>

<Result> attribute: 1. Name. The default value is success. Returned results of corresponding method execution
2. type. The default value is dispatcher, indicating the jump mode.
Dispatcher: forward to JSP
Redirect: redirects to a local machine or other JSP or action
Redirectaction: redirect to local action
Chain: forward to action


Dynamic results: 1. Add a string attribute to the action to record the target URL.
2. assign a value to this attribute dynamically in the method.
3. In struts. XML, the URL of the result of the action tag uses $ {attribute name}


Problem: duplicate URLs <form action = "User/login">
Public String login (){
Return input;
}
<Result name = "input">/login. jsp </result>
When the form submission fails, the URL address is not changed because the default type of result is forwarding;
When you submit the statement again next time, the/user will appear again.
Solution:
1. Use the absolute path for the action to submit the form. Add request. getcontextpath ()
2. Change the type in result to redirection.


Set global results and global exceptions:
<Package name = "base" extends = "struts-Default" namespace = "/">
<Global-Results>
<Result name = "error"> error. jsp </result>
</Global-Results>
<Global-exception-mappings>
<Exception-mapping result = "error" exception = "Java. Lang. nullpointerexception"> </exception-mapping>
</Global-exception-mappings>
</Package>




How to obtain requests and sessions encapsulated by struts2, and apply actioncontext CTX = actioncontext. getcontext ();
Map <string, Object> request = (Map <string, Object>) CTX. Get ("request ");
Map <string, Object> session = CTX. getsession ();
Map <string, Object> application = CTX. getapplication ();

You can also obtain


How to get the request, session, and application values in JSP: 1. Use the JSP built-in object
2. El expression
3. stuts Tag: <s: property value = "[# request. Key] [# ATTR. Key]"/>




Method 1:
Httpservletrequest request = servletactioncontext. getrequest ();
Httpsession session = request. getsession ();
Servletcontext application = servletactioncontext. getservletcontext ();

Method 2:
Through the implementation interface servletrequestaware and servletcontextaware, you can also obtain


Jstl: <C: If test = ""> </C: If>
<C: foreach items = "" Var = ""> </C: foreach>
<C: Out value = ""> </C: Out>
<C: Set Var = "" value = ""> </C: Set>
<C: Remove Var = ""> </C: Remove>

Ognl knowledge point ognl access value Stack: Object Name. attribute name
Attributes defined in action

Ognl access stack context (value stack context): # scope. attribute name
Scope




Ognl access list: list [0], list. Get (0)
Ognl Access Map: Map ['key'], map. Key,
Retrieve all keys: map. Keys
Retrieve all values: map. Values

Ognl access set: Set. toarray () [0]


Ognl calls static methods and attributes:
@ Class fully qualified name @ static method name
@ Class fully qualified name @ static attribute name
Add constant configuration in struts. xml:
Struts. ognl. allowstaticmethodaccess = true





1. The jar package that comes with Struts:
Antlr-2.7.6.jar and anltr-2.7.2.jar
2. 3 lib of Hibernate
ASM. Jar
Asm-attrs.jar
Cglib-2.1.3.jar
Commons-collections-2.1.1 and commons-collections-3.2
Commons-logging-1.0.4 and commons-logging-api-1.1
Find the jar package with the same name and delete the lower version



Struts2 Tag: DATA tag:
1. <s: property value = "ognl expression" default = "" escapehtml = ""/>
2. <s: Debug/>
3. <s: Date name = "ognl expression" format = "yyyy-mm-dd hh: mm: SS"/>
4. <s: Set Var = "variable name" value = "ognl expression" Scope = "scope, default value: Action"/> --- the ognl path is too long, or the execution result of a method is reused.
5. <s: URL value = "Address" Var = "T"/> the current string with the VaR attribute is not displayed.
6. <s: a href = "string"> </s: A>. To enter ognl in href, convert the string to an ognl expression, % {# t}
7. <s: Include value = "jsp to be included"/> dynamic inclusion

To convert: 1. string to ognl: --> % {}
The default attribute is string: s: A-href, S: URL-value, S: Include-value.
2. Convert ognl to a string:
This attribute is ognl: ---> enclosed by quotation marks by default.
S: Property-value,
S: Property-default,
S: Date-name
S: param-Value
If you are not sure whether this attribute is ognl by default, you can use % {} to guarantee processing as an ognl expression.

Control label:
1. <s: If test = "ognl expression"> </S: If>
2. <s: else> <s: elseif>
3. <s: iterator Var = "Current element" value = "set" status = "set status">
Take the attribute of the current element: <s: property value = "# current element. attribute name"/>
Judge whether the current index is odd: <s: If test = "# Set status. Odd"> </S: If>
Judge whether the current index is even: <s: If test = "# Set status. Even"> </S: If>
</S: iterator>
Use jquery to change the color of the line
$ (Function (){
$ ("Table TR: GT (0): Even" ).css ("background", "yellow ");
});



Ui Tag:
<S: Form theme = "topic" Action = "" method = ""> </S: Form>
<S: textfield name = "" value = ""/>
<S: textarea name = "" value = ""/>
<S: Submit/>
<S: Select name = "" list = "ognl expression" listkey = "attribute of value" listvalue = "attribute of display text"/>

<S: doubleselect
Name = "" list = "ognl expression 1" listkey = "attribute as value" listvalue = "attribute as display text"
Doublename = "" doublelist = "ognl expression 2" doublelistkey = "attribute as value" doublelistvalue = "attribute as display text"/>

Note: ognl expression 2 should be a set retrieved Based on ognl expression 1, such as map. Get (top)

Calendar control: <s: datetimepicker name = ""/>
Import steps: 1. Import struts-dojo-xxx.jar
2. Introduce the taglib on JSP:
<% @ Taglib uri = "/Struts-Dojo-tags" prefix = "SX" %>
3. Add tags to the head to introduce the relevant CSS and JS files.
<SX: Head parsecontent = "true"/>
4. Use the calendar Control

Step 1. Add an interceptor class to inherit abstractinterceptor and perform operations before and after intercept.
Intercept (actioninvocation Invocation ){
// 1. Pre-interception operations
// 2. Perform subsequent operations
String result = invocation. Invoke ();
// 3. Operation after Interception
Return result;
}
2. In the Struts. xml package, add the interceptor
<Interceptors>
<Interceptor name = "Interceptor 1" class = "Full qualified name of the class"/>
<Interceptor name = "Interceptor 2" class = "Full qualified name of the class"/>

<Interceptor-stack name = "Interceptor stack name">
<Interceptor-ref name = "defaultstack"/>
<Interceptor-ref name = "Interceptor 1"/>
<Interceptor-ref name = "Interceptor 2"/>
</Interceptor-stack>
</Interceptors>

3. In the Struts. xml action, specify the interceptor
<Interceptor-ref name = "Interceptor | interceptor stack"/>
If you want to use the struts default interceptor stack, you need to add the following:
<Interceptor-ref name = "defaultstack"/>




Form Content: Student ID, name, and photo

Struts implements file upload: single file:
1. Add a file field to the form. The form submission method should be post, and the enctype attribute should be multipart/form-data.
2. Add commons-fileupdate **. jar and commons-io **. jar.
3. In action, add three attributes based on the file domain name.
File xxx;
String xxxcontenttype;
String xxxfilename;
4. Save the file in the Action Method
String uploadpath = servletactioncontext. getservletcontext (). getrealpath ("/upload ");

Fileoutputstream Fos = new fileoutputstream (uploadpath + "/" + xxfilename );
Fileinputstream FCM = new fileinputstream (XXX );
Ioutils. Copy (FCM, FOS );


Restrict the file upload size:
1. Set global Constants
<Constant name = "struts. multipart. maxsize" value = "5000000"/>

2. Add Interceptor: restrict the file upload size of a single action
<Intercepter-ref name = "fileupload">
<Param name = "maximumsize"> 5000000 </param>
</Intercepter>
<Intercepter-ref name = "defaultstack"/>

The default value of the global constant is 2 m. If you want to limit the file size to less than 2 m, either of the two methods;
To limit the file size to 2 MB, all constants must be set to greater than or equal to the target size. Optional.



Restrict File Upload types:
Add parameters to the fileupload Interceptor:
<Intercepter-ref name = "fileupload">
<Param name = "allowedtypes"> image/JPEG, image/pjpeg, image/PNG, image/GIF </param>
</Intercepter>
<Intercepter-ref name = "defaultstack"/>


If an error occurs: HTTP status 404-no result defined for Action com. Pb. Web. Action. uploadaction and result Input
If the result is input, the corresponding result is not defined in the action.


File file field, only value can be taken, and value cannot be assigned

Struts2 Study Notes

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.