Struts2学习(第二篇)——开发第一个Struts2应用

本文来自:曹胜欢博客专栏。转载请注明出处:http://blog.csdn.net/csh624366188   

 

          前面一篇博客(细谈struts2之自己实现struts2框架)带大家对基于mvc业务流程熟悉了一下,现在我们就用对mvc实现最好的框架struts2来开发一个应用实例。虽然现在MyEclipse8.5以上版本已经开始支持Struts2但为了我们能更好的熟悉开发struts2的业务流程,现在我们还是手动去搭配环境。首先我们需要到struts.apache.org去下载struts-2.2.3-all包。现在最高版本应该达到2.3了。要想正常使用Struts2,至少需要如下五个包(可能会因为Struts2的版本不同,包名略有差异,但包名的前半部是一样的)。

    struts2-core-2.0.11.1.jar

    xwork-2.0.4.jar

    commons-logging-1.0.4.jar

    freemarker-2.3.8.jar

ognl-2.6.11.jar

注:貌似好像一些高版本的还需要加入一些其他jar包,如下图所示:

  

好了,jar包加入之后,我们下一步开始搭配配置环境了。很多同学可能会有这样的疑问,为什么我提交的请求能到struts.xml去找对应的action呢??至少我刚开始学习的时候有这么个疑问。现在答案即可以为大家揭晓了,因为struts2的核心是拦截器,一切请求都要经过拦截器才转发给所对应的action的。Struts2中第一个拦截请求的就是org.apache.struts2.dispatcher.FilterDispatcher这个拦截器(下一篇博客我们即将对这个拦截器的源码进行分析),拦截器对请求进行一些处理之后然后去struts.xml寻找对应的action。我们一起来看一下web.xml的配置:


[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.     <filter>  
  8.     <filter-name>struts2</filter-name>  
  9.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  10.     </filter>  
  11.     <filter-mapping>  
  12.     <filter-name>struts2</filter-name>  
  13.     <url-pattern>*.action</url-pattern>  
  14.     </filter-mapping>  
  15.   <welcome-file-list>  
  16.     <welcome-file>index.jsp</welcome-file>  
  17.   </welcome-file-list>  
  18. </web-app>  

      在struts2官方提供的文档中要求,在服务器class文件夹下建立struts.xml文件。由于在web项目部署到服务器上,开启服务器对web项目进行编译时会自动把src文件夹下的文件加载到服务器class文件夹下,所以我们直接在src下面建立struts.xml文件,具体struts.xml配置如下:

[html]  view plain copy print ?
  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. <constant name="struts.i18n.encoding" value="utf-8" />  
  7.     <package name="struts2" extends="struts-default">  
  8.         <action name="" class="">  
  9.             <result name=""></result>  
  10.             <result name=""></result>  
  11.         </action>  
  12.     </package>  
  13. </struts>     

注:上述代码具体意义:

1.<constant>标签主要是用来修改struts.properties配置文件信息,namevalue分别相当于struts.properties文件中的namevalue

2.<package>主要是作为分包作用,比如一个项目分好几个模块,这里可以每一个模块分配一个包,一个struts.xml文件中可以出现多个<package>标签,这里一定要有extends="struts-default",因为struts的核心拦截器都配置在struts-default包中,如果没有这个,所有请求都不会请求到

3.一个<action>标签对应一个action类,主要是通过action标签中的去寻找class,然后执行对应的classAction标签里有一个一个method属性,他可以指定执行action中的哪个方法

下面我们就开始以登录为例来写一下struts2开发的视图层:

Login.jsp

[html]  view plain copy print ?
  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. <html>  
  7.   <head>  
  8.   </head>  
  9.     <body>  
  10.   <form action="LoginAction.action">  
  11.    <input type="text" name="username"/><br/>  
  12.    <input type="password" name="password"/><br/>  
  13.    <input type="submit" value="提交"/>  
  14.    </form>  
  15.   </body>  
  16. </html>  

好了,视图层写完了,下面就要开始写核心action了,由于业务逻辑层就是判断用户名和密码是否与固定的admin123456相等,所以本程序只是为了测试,就不再单独抽出来了,直接写在action里面了LoginAction.java

[java]  view plain copy print ?
  1. package com.bzu.action;  
  2. public class LoginAction {  
  3.       
  4.     private String username;  
  5.     private String password;  
  6.     public String getUsername() {  
  7.         return username;  
  8.     }  
  9.     public void setUsername(String username) {  
  10.         this.username = username;  
  11.     }  
  12.     public String getPassword() {  
  13.         return password;  
  14.     }  
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18.     public String execute(){  
  19.           
  20.         if(username.equals("admin")&&password.equals("123456"))  
  21.             return "success";  
  22.         return "fail";  
  23.     }  
  24. }  


从上面的程序可以看出,我们在action中要把form表单中数据都以私有变量的形式定义出来,然后在提供对应的setget方法。很多同学可能在这又有疑问了。为什么给他提供setget方法,form表单中的数据就可以设置到对应的属性上呢?为什么他会默认的去执行execute方法呢?为什么把配置文件中action标签对应的method属性修改后就可以执行新设置的方法呢?呵呵,在这在卖个关子,在接下来的博客中,会为大家一一把这些疑问解决。

Action写完之后,我们就可以把struts.xml对应的写上了,本程序完整的struts.xml

[html]  view plain copy print ?
  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. <constant name="struts.i18n.encoding" value="utf-8" />  
  7.     <package name="struts2" extends="struts-default">  
  8.         <action name="LoginAction" class="com.bzu.action.LoginAction">  
  9.             <result name="success">success.jsp</result>  
  10.             <result name="fail">fail.jsp</result>  
  11.         </action>  
  12.     </package>  
  13. </struts>     
  14.    

 

对应的success.jspfai.jsp没什么内容,就是显示成功和失败几个字。

好了,到此,我们的第一个struts2的应用程序就写完了,下面我们一起来看一下运行结果:

————》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值