【SSH】 之 Struts2环境搭建及简单应用开发

在上一篇文章中,我们一起了解了一下struts2的工作机制原理,接下来让我们进行一下简单应用的开发

(一)配置环境

1、建立web项目

              

 2、导入jar包

          

       其中struts2中有很多jar包,我们不需要全部引用,因为很多jar涉及第三方jar包。如果我们只导入struts里面的而没有导入第三方jar包所依赖的jar包,就会报错,影响开发

         

3、配置web.xml

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.      
  8.    <!-- 配置Struts2的核心过滤器 -->  
  9.  <filter>  
  10.     <filter-name>struts2</filter-name>  
  11.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     <!-- 注意在2.1.3以上版本需使用此class -->    
  13.  </filter>  
  14.    
  15.  <filter-mapping><!-- 配置url路径 -->    
  16.     <filter-name>struts2</filter-name>  
  17.     <url-pattern>/*</url-pattern>  
  18.     <!-- <dispatcher>REQUEST</dispatcher>  
  19.     <dispatcher>FORWARD</dispatcher> -->  
  20.  </filter-mapping>  
  21.   <display-name></display-name>   
  22.   <welcome-file-list>  
  23.     <welcome-file>index.jsp</welcome-file>  
  24.   </welcome-file-list>  
  25. </web-app>  

 

 

 4、配置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.     
  7. <struts>    
  8.       <package name="StrutsTest" namespace="/Test" extends="struts-default">    
  9.         <action name="helloworld" class="/StrutsTest/src/Test/HelloWorld" method="execute">    
  10.             <result name="success">/WEB-INF/jsp/hello.jsp</result>    
  11.         </action>    
  12.     </package>    
  13. </struts>  

 

 

配置完成后,启动项目,没有报异常则证明环境搭建成功。

 

(二)进行简单应用开发

1、对应的Action类

         

  1. package Test;  
  2. public class HelloWorld {  
  3.     private String info;  
  4.       
  5.     public String getInfo(){  
  6.         return info;          
  7.     }  
  8.       
  9.     public String execute(){  
  10.         info="电话:18333617223"+"\n"+"姓名:李卫中";  
  11.         return "success";         
  12.     }  
  13. }  

 

 

2、配置对应的jsp视图

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>    
  2. <%    
  3. String path = request.getContextPath();    
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
  5. %>      
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
  7. <html>    
  8.   <head>    
  9.     <base href="<%=basePath%>">         
  10.     <title>struts</title>         
  11.     <meta http-equiv="pragma" content="no-cache">    
  12.     <meta http-equiv="cache-control" content="no-cache">    
  13.     <meta http-equiv="expires" content="0">        
  14.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    
  15.     <meta http-equiv="description" content="This is my page">      
  16.   </head>        
  17.   <body>    
  18.     ${info}  <br>    
  19.   </body>    
  20. </html>    

 

 

(三)运行:

                   

简单应用开发成功

 

小结:

        struts2在环境配置上相对简单,开发应用上也比较容易,接下来,我会就struts与struts2的相关学习做出比较,对比一下struts与struts2的应用方向与应用场景,以及两者之间的异同点。

posted @ 2016-09-03 16:42  Double-Eggs  阅读(236)  评论(0编辑  收藏  举报