准备工作:

从中提取出9个核心包(包名以第一个截图为准):
clipboard.png

核心包作用:

clipboard.png

clipboard.png

开始配置:

  • 创建java动态web工程,命名为struts2。

clipboard.png

  • 创建用户库

,将Struts2所需的包建成用户库,可以更加方便地进行管理和使用。打开Eclipse,选择菜单Window->Preferences->Java->Build Path->User Libraries
clipboard.png
将需要的包(struts2.3.32)通过add library加入工程:
clipboard.png
将struts2-blank.war工程覆盖原工程下的webroot(webContent)文件

  • 配置struts.xml:

clipboard.png

   
   
    <?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">   
    <struts>   
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
    <constant name="struts.devMode" value="true" />   
    <package name="default" namespace="/" extends="struts-default">  
        <action name="hello" class="struts.Test">  
            <result>/index.jsp</result>  
        </action>  
        <action name="hello2" class="struts.Test">  
            <result>/example/HelloWorld.jsp</result>  
        </action>  
    </package>  
</struts>  

  • 配置web.xml:

       <?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>
    
       <welcome-file-list>
           <welcome-file>index.html</welcome-file>
       </welcome-file-list>
       
       <!-- Restricts access to pure JSP files - access available only via Struts action -->  
       <security-constraint>  
           <display-name>No direct JSP access</display-name>  
           <web-resource-collection>  
               <web-resource-name>No-JSP</web-resource-name>  
               <url-pattern>*.jsp</url-pattern>  
           </web-resource-collection>  
           <auth-constraint>  
               <role-name>no-users</role-name>  
           </auth-constraint>  
       </security-constraint>  
       

    </web-app>

  • 创建strus包,Test.java.

Test.java:

package struts;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class Test extends ActionSupport {  
        
    public String execute() throws Exception {  
            return  SUCCESS;  
   }  
}  

clipboard.png

开启tomcat,配置完成从检验:
clipboard.png

注意:工程此时有两个lib,分别是libraries下的strust2与WEB-INFO下的lib,可根据需要删除WEB-INFO下的lib。

muddyway
10 声望3 粉丝