Struts-2.3.16 环境配置

来源:互联网 发布:被收购的中国品牌 知乎 编辑:程序博客网 时间:2024/04/30 11:19
必要的jar包:

struts2-core-2.3.16.jar

xwork-core-2.3.16.jar

commons-logging-1.1.3.jar

ognl-3.0.6.jar

commons-fileupload-1.3.jar

freemarker-2.3.19.jar

commons-io-2.2.jar

javassist-3.11.0.GA.jar

commons-lang-2.4.jar

commons-lang3-3.1.jar

如需跟spring整合需添加struts2-spring-plugin-2.3.16.jar

 JAR 包下载地址:少年,快点我

修改web.xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- struts2 start  -->    
  2.     <filter>  
  3.         <filter-name>struts2</filter-name>  
  4.         <filter-class>  
  5.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  6.         </filter-class>  
  7.     </filter>  
  8.     
  9.    <filter-mapping>  
  10.         <filter-name>struts2</filter-name>  
  11.         <url-pattern>/*</url-pattern>  
  12.     </filter-mapping>  
  13.       
  14.     <!-- struts2 end -->  

src下添加struts.xml文件

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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.      <constant name="struts.i18n.encoding" value="UTF-8" />  
  8.     <!-- <constant name="struts.objectFactory" value="spring" />  bean交spring管理 -->  
  9.      <constant name="struts.enable.DynamicMethodInvocation" value="false" />  <!-- 为true可使用感叹号调用方法,官网不推荐 -->  
  10.      <include file="config/struts2/struts-*.xml"></include>  <!-- 加载src/config/struts2/ 目录下所有struts文件 注意命名格式 -->  
  11. </struts>  

新建类 Hello.java

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com;  
  2.   
  3. public class Hello {  
  4.   
  5.     private String message;  
  6.       
  7.       
  8.     public String method(){  
  9.         System.out.println("进入method" + this);  
  10.         return "msg";  
  11.     }  
  12.       
  13.       
  14.   
  15.     public String getMessage() {  
  16.         return message;  
  17.     }  
  18.   
  19.     public void setMessage(String message) {  
  20.         this.message = message;  
  21.     }  
  22. }  

src/config/struts2/新建struts-test.xml 文件

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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.        
  8.     <package name="test1" namespace="/test" extends="struts-default">  
  9.       
  10.         <action name="hello_*" class="com.Hello" method="{1}">  
  11.               <result name="msg">/index.jsp</result>  
  12.         </action>  
  13.       
  14.     </package>  
  15.         
  16. </struts>  

index.jsp 内容<body>里面添加 ${ message}

访问http://localhost:8080/项目名/test/hello_method.action即可;

0 0
原创粉丝点击