Struts 2.1 简单示例(手写)

对于技术原理不作解析, 我的目标是让需要的朋友能一次性成功完成自己的第一个Struts 2.1项目.

 

1. 添加Apache Struts 2.1.6库

 

commons-fileupload-1.2.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar

 

与Struts 2.0相比, 多了commons fileupload.

下载地址: http://struts.apache.org/download.cgi#struts216

 

2. 在web.xml中配置FilterDispatcher

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  5.     <filter>  
  6.         <filter-name>struts2.1</filter-name>  
  7.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  8.     </filter>  
  9.     <filter-mapping>  
  10.         <filter-name>struts2.1</filter-name>  
  11.         <url-pattern>/*</url-pattern>  
  12.     </filter-mapping>  
  13. </web-app>  

 

 

3. 编写HiAction

 

Java代码  收藏代码
  1. package mp.likeming.action;  
  2.   
  3. public class HiAction {  
  4.   
  5.     private String name;  
  6.       
  7.     public String execute() {  
  8.         return "success";  
  9.     }  
  10.   
  11.     /** 
  12.      * @return the name 
  13.      */  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     /** 
  19.      * @param name the name to set 
  20.      */  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.   
  25. }  

 

4. 编写页面index.jsp及hi.jsp

 

index.jsp:

 

Html代码  收藏代码
  1. <%@page contentType="text/html; charset=UTF-8" %>  
  2. <%@taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  7. <title>《开始Struts 2.1》 之 Struts 2.1 简单示例</title>  
  8. <style type="text/css">  
  9. <!--  
  10. * {  
  11.     font-family:"Microsoft Yahie";  
  12.     font-size:16px;  
  13. }  
  14. body {  
  15.     text-align:center;  
  16. }  
  17. -->  
  18. </style>  
  19. </head>  
  20. <body>  
  21. <s:form action="hiAction">  
  22.   称呼: <s:textfield name="name" /> <s:submit value="提交" />  
  23. </s:form>  
  24. </body>  
  25. </html>  

 

 

hi.jsp:

 

Html代码  收藏代码
  1. <%@page contentType="text/html; charset=UTF-8" %>  
  2. <%@taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  7. <title>《开始Struts 2.1》 之 Struts 2.1 简单示例</title>  
  8. <style type="text/css">  
  9. <!--  
  10. * {  
  11.     font-family:"Microsoft Yahie";  
  12.     font-size:16px;  
  13. }  
  14. body {  
  15.     text-align:center;  
  16. }  
  17. .name {  
  18.     font-weight:600;  
  19. }  
  20. -->  
  21. </style>  
  22. </head>  
  23. <body>  
  24. <p>Hi, <span class="name"><s:property value="name" /></span></p>  
  25. <p>感谢您关注Struts 2.1</p>  
  26. </body>  
  27. </html>  

 

 

5. 编写struts.xml

 

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. <struts>  
  6.     <!-- 主题设置 -->  
  7.     <constant name="struts.ui.theme" value="simple" />  
  8.   
  9.     <package name="stuts2" extends="struts-default">  
  10.         <action name="hiAction" class="mp.likeming.action.HiAction">  
  11.             <result>hi.jsp</result>  
  12.         </action>  
  13.     </package>  
  14. </struts>  

 

 

附件为项目编译版, 可部署至Tomcat运行, 以查看运行效果.

如有不到之处, 请一定批评指正. 非常感谢!!!

 

转自:http://jalx.iteye.com/blog/339061

posted @ 2014-03-18 14:27  losesea  阅读(241)  评论(0)    收藏  举报