The first is the web. xml configuration, which is the start of project loading.
1 <display-name> </display-name> 2 <! -- Struts2 configuration start --> 3 <filter> 4 <filter-name> struts2 </filter-name> <! -- Consistent with the name in mapping --> 5 <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> 6 </filter> 7 <filter-mapping> 8 <filter-name> struts2 </filter-name> 9 <url-pattern> *. do </url-pattern> 10 <url-pattern> *. jsp </url-pattern> 11 </filter-mapping> 12 <! -- Struts2 configuration ended --> 13 <welcome-file-list> 14 <welcome-file> index. jsp </welcome-file> 15 </welcome-file-list>
Then the struts. xml file under src is the source of the struts configuration file.
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE struts PUBLIC 3 "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <! -- Encoding --> 8 <constant name = "struts. i18n. encoding" value = "UTF-8"/> 9 <! -- Extension --> 10 <constant name = "struts. action. extension" value = "do, action"/> 11 <! -- Whether the browser caches static data --> 12 <constant name = "struts. serve. static. browserCache" value = "false"/> 13 <! -- The struts configuration file is automatically loaded after modification --> 14 <constant name = "struts. configuration. xml. reload" value = "true"/> 15 <! -- Print out more detailed error information in development mode: true false --> 16 <constant name = "struts. devMode" value = "true"/> 17 <! -- Default subject --> 18 <constant name = "struts. ui. theme" value = "simple"/> 19 <! -- Whether dynamic call is supported --> 20 <constant name = "struts. enable. DynamicMethodInvocation" value = "false"/> 21 <! -- Size of the uploaded file --> 22 <constant name = "struts. multipart. maxSize "value =" 2097152 "/> 23 24 <package name =" base "extends =" struts-default "> </package> 25 <include file =" strutsxml/struts. xml "> </include> <! -- The value can be changed. Just change it to your corresponding file --> 26 <include file = "strutsxml/struts_ajax.xml"> </include> <! -- You can load multiple sub-configuration files --> 27 </struts>
After the master configuration file is loaded, the sub-configuration file is loaded. Write one here, and the other is dispensable. Check the project structure.
Strutsxml/struts. xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE struts PUBLIC 3 "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <package name = "default" extends = "base" namespace = "/"> 8 <! -- The class value corresponds to the java class, And the execute method is called by default. The attribute method can be added, the value is the method name. The call method has been modified. --> 9 <action name = "Categories" class = "com. junjuping. ui. action. categories "> 10 <result name =" succ ">/CategoriesList. jsp </result> <! -- Corresponding to the jsp file; the value of name is the string returned by the java class --> 11 </action> 12 </package> 13 </struts>
The action tag of the sub-configuration file can also be directly stored in the main configuration file (package tag ).
By default, the execute method in the corresponding java class is called.
Com. junjuping. ui. action. Categories. java
1 public class Categories 2 {3 public String execute () throws Exception {4 String result = ""; // return value 5 HttpServletRequest request = ServletActionContext. getRequest (); 6 HttpServletResponse response = ServletActionContext. getResponse (); 7 CategoriesServce categoriesServce = new CategoriesServce (); 8 ArrayList <String []> resultList = categoriesServce. select (); 9 request. setAttribute ("resultList", resultList); 10 result = "succ"; 11 return result; 12 }}
The jsp file is not described here.