struts2总结之一

Struts2框架发展
Apache Struts 在 2000年5月由Craig McClanahan 发起,并于2001年7月发布了1.0版。
Struts一出现便大受欢迎,更成为了以后几年内web 开发的实际标准。
Struts2是Struts的下一代产品,是在WebWork的技术基础上进行了合并。
Struts2是在WebWork2基础发展而来的。主要是因为struts2有以下优点:
1 > 在软件设计上Struts2没有像struts1那样跟Servlet API和struts API有着紧密的耦合,Struts2的应用可以不依赖于Servlet API和struts API。 Struts2的这种设计属于无侵入式设计,而Struts1却属于侵入式设计。
2> Struts2提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能。
3> Strut2提供了类型转换器,我们可以把特殊的请求参数转换成需要的类型。在Struts1中,如果我们要实现同样的功能,就必须向Struts1的底层实现BeanUtil注册类型转换器才行。
4> Struts2提供支持多种表现层技术,如:JSP、freeMarker、Velocity等
5> Struts2的输入校验可以对指定方法进行校验,解决了Struts1长久之痛。
6> 提供了全局范围、包范围和Action范围的国际化资源文件管理实现
搭建Struts2环境时,我们一般需要做以下几个步骤的工作:
1》找到开发Struts2应用需要使用到的jar文件.
2》编写Struts2的配置文件
3》在web.xml中加入Struts2 MVC框架启动配置
<package name="itcast" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >
<result name="success">/hello.jsp</result>
</action>
</package>
在struts2框架中使用包来管理Action,包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action。在实际应用中,我们应该把一组业务功能相关的Action放在同一个包下。
配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,他不对应java的类包,如果其他包要继承该包,必须通过该属性进行引用。包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下的Action的路径的一部分,如访问上面例子的Action,访问路径为:/test/helloworld.action。 namespace属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串)。

通常每个包都应该继承struts-default包, 因为Struts2很多核心的功能都是拦截器来实现。如:从请求中把请求参数封装到action、文件上传和数据验证等等都是通过拦截器实现的。 struts-default定义了这些拦截器和Result类型。可以这么说:当包继承了struts-default才能使用struts2提供的核心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。 struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。
包还可以通过abstract=“true”定义为抽象包,抽象包中不能包含action。
Action名称的搜索顺序:
1.获得请求路径的URI,例如url是:http://server/struts2/path1/path2/path3/test.action
2.首先寻找namespace为/path1/path2/path3的package,如果存在这个package,则在这个package中寻找名字为test的action,如果不存在这个package则转步骤3;
3.寻找namespace为/path1/path2的package,如果存在这个package,则在这个package中寻找名字为test的action,如果不存在这个package,则转步骤4;
4.寻找namespace为/path1的package,如果存在这个package,则在这个package中寻找名字为test的action,如果仍然不存在这个package,就去默认的namaspace的package下面去找名字为test的action(默认的命名空间为空字符串"" ),如果还是找不到,页面提示找不到action。
Action配置中的各项默认值:
<package name="itcast" namespace="/test" extends="struts-default">
        <action name="helloworld" class="cn.itcast.action.HelloWorldAction"  method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
  </package>
1>如果没有为action指定class,默认是ActionSupport。
2>如果没有为action指定method,默认执行action中的execute() 方法。
3>如果没有指定result的name属性,默认值为success。
用struts2完成登录案例:
在index.jsp中:
<body>
  <div align="center">
     <h1>用户登录界面</h1>
     <div>${msg}</div>
     <div>
      <form action="${pageContext.request.contextPath}/csdn/loginAction" method="post">
        <table>
           <tr>
             <td> 用户名</td>
             <td><input type="text" name="username"/>
             </td>
           </tr>
           <tr>
            <td>密码</td>
            <td>
              <input type="password" name="userpass"/>
            </td>
           </tr>
           <tr>
             <td colspan="2">
              <input type="submit" value="登录">
              &nbsp;&nbsp;&nbsp;&nbsp;
              <input type="reset">
             </td>
           </tr>
        </table>
      </form>
      </div>
      </div>
  </body>
在LoginAction类中
public class LoginAction implements Action{
  private String username;
  private String userpass;
  private String msg;
注意:此处省略了get,set方法
public String login(){
System.out.println("你传递的参数值是:"+username+"----------"+userpass);
if("feng".equals(username)){
return SUCCESS;
}
this.setMsg("重新登录");
return INPUT;
}
}
Struts2的配置文件中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package name="redarmys" extends="struts-default" namespace="/csdn">
    <action name="loginAction" class="cn.csdn.action.LoginAction"  method="login">
       <result name="success">/sc.jsp</result>
       <result name="input">/index.jsp</result>
    </action>
  </package>
</struts>

猜你喜欢

转载自1150689774-qq-com.iteye.com/blog/1027788