Struts2学习笔记-part1: 快速起步

简介:
建立一个Java Web项目,提取最少运行Struts2应用的包集合(摘自Struts官方文档):

Install the Minimum Set of Libraries and Configuration Files

The following files are a minium requirement for your application.
Filename Description
struts2-core.jar Framework library itself, found in distribution root directory
xwork.jar XWork 2 library on which Struts 2 is built (version 2.0 or later)
ognl.jar Object Graph Navigation Language (OGNL), the expression language used throughout the framework
freemarker.jar All UI tag templates are written in Freemarker (also a good option for your own views)
commons-logging.jar Commons logging, which the framework uses to support transparently logging to either Log4J or JDK 1.4+
web.xml Java web application configuration file that defines the filters (and other components) for your web application
struts.xml Framework configuration file that defines the actions, results, and interceptors for your application
If any Struts 2 Plugins are included, then other JARs may be needed too. For example, the optional Spring Plugin requires the Spring JARs to be present.
 
 
目标:实现一个简单的用户登录.
 
下面是实现源码:
 
一、先实现登录页面
<%-- 
  登录页面 
  User: leizhimin 
  Date: 2008-3-14 
  Time: 15:45:52 
  To change this template use File | Settings | File Templates. 
--%> 
< %@ page  contentType ="text/html;charset=GBK"  language ="java" % > 
< html > 
< head > < title >登录页面 </title> </head> 

< body > 
< form  action ="Login.action"  method ="post" > 
     < table  align ="center" > 
         < caption > < h3 >用户登录 </h3> </caption> 
         < tr > 
             < td >用户名: < input  type ="text"  name ="username" /> </td> 
         </tr> 
         < tr > 
             < td >密  码: < input  type ="text"  name ="password" /> </td> 
         </tr> 
         < tr  align ="center" > 
             < td  align ="center" > 
                 < input  type ="submit"  value ="登录" /> 
                 < input  type ="reset"  value ="重填" /> 
             </td> 
         </tr> 
     </table> 
</form> 
</body> 
</html>
 
二、实现处理页面的Action
 
package stu; 

/** 
* 处理用户请求的Action 
* File: LoginAction.java 
* User: leizhimin 
* Date: 2008-3-14 15:59:07 
*/
 
public  class LoginAction { 
     private String username; 
     private String password; 

     /** 
     * 处理用户请求的execute方法 
     * 因为Struts2的拦截机制,他们负责解析用户的请求参数,并将请求参数赋给Action对应的属性 
     * 
     * @return 当用户名为aaa并且密码为123时,返回success,否则,返回error. 
     * @throws Exception 
     */
 
     public String execute()  throws Exception { 
         //当用户名为aaa并且密码为123时,返回success,否则,返回error. 
         if (getUsername().equals( "aaa") && getPassword().equals( "123")) { 
             return  "success"
        }  else { 
             return  "error"
        } 
    } 

     public String getPassword() { 
         return password; 
    } 

     public  void setPassword(String password) { 
         this.password = password; 
    } 

     public String getUsername() { 
         return username; 
    } 

     public  void setUsername(String username) { 
         this.username = username; 
    } 
}
 
三、配置Web.xml
 
<? xml  version ="1.0"  encoding ="GBK" ?> 

< web-app  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 [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]" > 

     < display-name >Struts Blank </ display-name > 
    <!-- 定义Struts2的FilterDispatcher的Filter--> 
     < filter > 
        <!-- 定义核心Filter的名字--> 
         < filter-name >struts2 </ filter-name > 
        <!-- 定义核心Filter的实现类--> 
         < filter-class >org.apache.struts2.dispatcher.FilterDispatcher </ filter-class > 
     </ filter > 
    <!-- FilterDispatcher用来初始化Struts2并且处理所有的Web请求--> 
     < filter-mapping > 
         < filter-name >struts2 </ filter-name > 
         < url-pattern >/* </ url-pattern > 
     </ filter-mapping > 

     < welcome-file-list > 
         < welcome-file >/login.jsp </ welcome-file > 
     </ welcome-file-list > 
</ web-app >
 
四、配置Action处理结果和资源资源之间的映射关系
        注意:此文件打包后位于WEB-INF/classes/目录下面,或者放入classpath。
 
<? xml  version ="1.0"  encoding ="GBK" ?> 

< web-app  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 [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]" > 

     < display-name >Struts Blank </ display-name > 
    <!-- 定义Struts2的FilterDispatcher的Filter--> 
     < filter > 
        <!-- 定义核心Filter的名字--> 
         < filter-name >struts2 </ filter-name > 
        <!-- 定义核心Filter的实现类--> 
         < filter-class >org.apache.struts2.dispatcher.FilterDispatcher </ filter-class > 
     </ filter > 
    <!-- FilterDispatcher用来初始化Struts2并且处理所有的Web请求--> 
     < filter-mapping > 
         < filter-name >struts2 </ filter-name > 
         < url-pattern >/* </ url-pattern > 
     </ filter-mapping > 

     < welcome-file-list > 
         < welcome-file >/login.jsp </ welcome-file > 
     </ welcome-file-list > 
</ web-app >
 
五、增加登录成功和失败页面
 
< %@ page  contentType ="text/html;charset=GBK"  language ="java" % > 
< html > 
   < head > < title >登录成功页面 </title> </head> 
   < body > 
           登录成功! 
   </body> 
</html>
 
< %@ page  contentType ="text/html;charset=GBK"  language ="java" % > 
< html > 
   < head > < title >登录失败 </title> </head> 
   < body > 
      登录失败! 
   </body> 
</html>
 
六、打包并部署到tomcat,运行如下图:
 
 
 


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/65773,如需转载请自行联系原作者
相关文章
|
前端开发 Java 编译器
Spring5新宠:PathPattern,AntPathMatcher:那我走?(下)
Spring5新宠:PathPattern,AntPathMatcher:那我走?(下)
Spring5新宠:PathPattern,AntPathMatcher:那我走?(下)
|
6月前
|
开发框架 前端开发 Java
Struts vs. Struts 2:Java Web 开发框架的升级之路与竞争力分析
Struts vs. Struts 2:Java Web 开发框架的升级之路与竞争力分析
44 0
|
11月前
|
XML Kubernetes 数据可视化
探索3种顶级「集成框架」Apache、Spring和Mule
探索3种顶级「集成框架」Apache、Spring和Mule
|
11月前
|
应用服务中间件
JavaWeb--快速入门Servlet(Part3)
JavaWeb--快速入门Servlet(Part3)
95 0
|
11月前
|
XML 开发框架 安全
JavaWeb--快速入门Servlet(Part1)
JavaWeb--快速入门Servlet(Part1)
63 0
|
前端开发 Java Spring
猿创征文|Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time.
猿创征文|Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time.
753 0
猿创征文|Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time.
|
缓存 NoSQL Java
SpringCloud升级之路2020.0.x版-5.所有项目的parent与spring-framework-common说明
SpringCloud升级之路2020.0.x版-5.所有项目的parent与spring-framework-common说明
SpringCloud升级之路2020.0.x版-5.所有项目的parent与spring-framework-common说明
|
Java API 网络架构
Spring5新宠:PathPattern,AntPathMatcher:那我走?(上)
Spring5新宠:PathPattern,AntPathMatcher:那我走?(上)
Spring5新宠:PathPattern,AntPathMatcher:那我走?(上)
|
XML 存储 Java
第7章—SpringMVC高级技术—处理multipart形式的数据
处理multipart形式的数据 MultipartResolver 用于处理文件上传,当收到请求时 DispatcherServlet 的 checkMultipart() 方法会调用 MultipartResolver 的 isMultipart() 方法判断请求中是否包含文件。
1267 0