I have encountered these problems during development. If you have different opinions, please note that you can exchange and learn.
1. When writing struts2, in return, remember that success and so on are defined in action. It is a constant. If it is written as the string literal value "success. If you have written a string literal value, you must go to <resutl name = "your string literal value"> XXX. jsp or xxx.html </result>
Note: This is case sensitive.
2. In web. XML, the standard filter statement in the latest version (Struts-2.2.3.1 as of now) is as follows: (the premise is that your web. XML format must be correct)
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app id = "webapp_9" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<Display-Name> struts blank </display-Name>
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
</Web-app>
3. many online comrades say that struts is used. XML is put under the SRC folder, but this is the case in eclipse. If it is directly put in deploy of Tomcat webapps, the default struts. XML should be placed under the webapp/WEB-INF/classes/folder, namely: webapp/WEB-INF/classes/struts. XML.
4. for the name attribute in <package> </package>, some comrades may misunderstand this attribute and think that its value is our Java package name. In fact, this name can be set arbitrarily, as long as it is not the same as the name in other packages. The only function is to tell the developer the unique name of the package.
5. The value of the name attribute in <action> </Action> must be consistent with that in the action in the form. If the value of name in <action> </Action> is "hello", the form should be written as "Hello. Action ".
However, the premise is that the namespace attribute in <package> </package> is not written, or namespace = "/".
If namespace = "/XXX/yyy ". It should be written in the form:
<Form action = "xxx/YYY/actionname. Action" method = "post | get"> </form>
Note: The namespace must start with "/"; otherwise, xxx action cannot be found. This is a bit similar to URL-pattern in servlet configuration.
6. When using expressions in the struts2 tag. Note:
A: To start:
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
B: In web. XML, add the core controller of struts2:
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
C: add the corresponding struts2 library in APP/WEB-INF/lib.
Yes.
<S: Set Name = "Age" value = "61"/>
<S: If test = "% {age> 60}">
Display result Contents
</S: If>
7. Internationalization:
A: Global:
Configure in APP/WEB-INF/classes/struts. xml:
<Constant name = "struts. Custom. i18n. Resources" value = "globalmessages"> </constant>
Then create an international resource file in the app/WEB-INF/classes/folder, basename_language_contry.properties such:
Globalmessages_en_us.properties
Globalmessages_zh_cn.properties
If you want to store international resource files in a specific folder, configure it like this (assuming you're in the app/WEB-INF/classes/i18n/folder:
Configure in APP/WEB-INF/classes/struts. xml:
<Constant name = "struts. Custom. i18n. Resources" value = "i18n/globalmessages"> </constant>
The files in the app/WEB-INF/classes/i18n/folder are:
Globalmessages_en_us.properties
Globalmessages_zh_cn.properties
B: You do not need to configure it in the configuration file:
Place your international resource file in APP/WEB-INF/classes/: XXXXX. Properties
Then, when calling in JSP:
<I18n name = "The/path/to/XXXXX">
<S: Text name = "your key"/>
</I18n>
Note: The attribute of name in <i18n> must be the full name of the international resource file (except for the suffix ". properties", do not enter the suffix name !).
C: dynamically changing internationalization (the premise is that you have to prepare these internationalization files): Add a parameter when requesting action:
XXXX. Action? Request_locale = zh_cn, which indicates Chinese characters.
XXXX. Action? Request_locale = en_us, which indicates English.
8. Labels:
A: Set
@
<S: Set Name = "username" value = "yangzhiyong">
</S: Set>
<S: If test = '{# username = "yangzhiyong"}'>
Welcome, Yang Zhi Yong!
</S: If>
<S: else>
Sorry, you are not Yang Zhi Yong!
</S: else>
@ # {"Key": "value"}. Pay attention to the single quotation marks and double quotation marks.
<S: Set Name = "username" value = '# {"first": "yang", "last": "Zhiyong"}'>
</S: Set>
<S: If test = '{# username ["first"] = "yang"}'>
Welcome, Yang!
</S: If>
<S: else>
Sorry, you are not Yang!
</S: else>
@ Compare the size with the number (note that no braces are needed ):
<S: Set Name = "year" value = "61">
</S: Set>
<S: If test = "# Name
> 61 "> (or written as <s: If test =" # name GT 61 ">)
More than 61
</S: If>
<S: elseif test = "# Name
<61 "> (or <s: elseif test =" # name lt 61 ">)
Less than 61
</S: elseif>
<S: else>
Equal 61
</S: else>
9. CSS failure during struts2 redirection:
Solution:
<%
String contextpath = request. getcontextpath (); // obtain the name of the currently running webapp.
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + contextpath;
Basepth returns http: // addrress: Port/webapp
Example: http: // localhost: 8080/mywebapp
Note that there is no "/" in the end ....
%>
10. Configure the interceptor
In struts. if no interceptor is added to the action in the XML file, the default interceptor will take effect. however, once an interceptor is added for the action in the package, the default interceptor will not work. in this case, you must manually add the default interceptor.
<Intercepter-ref name = "defaultstack"> </Interceptor-ref>
11. When you configure action in struts. XML, if the action with the same name (and in the same package) exists, only the last action takes effect.
12. Path Splitting: applicable to both UNIX and Linux.
- Since file. Separator in UNIX is a slash "/", the following code can handle all windows and Unix scenarios:
- String temp [] = Name. replaceall ("\\\\", "/"). Split ("/");
13.
If the fields in the action are the same as those in the model, the model takes precedence.
14. struts2 labels can be nested:
<S: iterator value = "# ATTR. listallofuserlogininfos" id = "userlogininfo" status = "obj_status">
<S: property value = "# ATTR. listpageofuserbloginfos. Get (# obj_status.count-1). blogname"/>
</S: iterator>
15. redirect action:
<action name="login" class="org.android.action.user.UserAction" method="login"> <result type="redirectAction" name="success"> <param name="actionName">listAllFilesAction</param> </result> <result name="input">/jsp/login.jsp</result> </action> <action name="listAllFilesAction" class="org.android.action.file.ListAllFilesAction" method="list"> <result name="success">/jsp/listOwnFiles.jsp</result> </action>
16. When HTML content is displayed in <s: property value = "XX"/>. Use the escape attribute to determine whether to parse HTML content.