zoukankan      html  css  js  c++  java
  • Struts2初体验

    1.使用Struts 2 开发程序的基本步骤

      加载Struts2 类库

      配置web.xml文件

         开发视图层页面

      开发控制层Action

      配置struts.xml文件

         部署、运行项目

     步骤1: 配置web.xml文件

      <filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

      </filter>

      <filter-mapping>

         <filter-name>struts2</filter-name>

         <!-- 拦截所有的action -->

         <url-pattern>/*</url-pattern>

      </filter-mapping>

      

    步骤2:在src下创建名称为struts.xml的配置文件

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

    <struts>

        <!-- 配置文件中只要添加以下配置,那么以后修改配置文件不用重启tomcat -->

        <constant name="struts.devMode" value="true" />

        <package name="default" namespace="/" extends="struts-default">

            <!-- 第一个action的例子 -->

            <action name="helloWorld" class="cn.happy.action.HelloWorldAction">

                <result name="success">

                   index.jsp

                </result>

            </action>

            <!-- 登录的action -->

        </package>

        <!-- Add packages here -->

    </struts>

    步骤3:编写HelloWorldAction

    package cn.happy.action;

    import com.opensymphony.xwork2.Action;

    public class HelloWorldAction implements Action{

    private String name ;

    private String message;

    public String execute() throws Exception {

    setMessage("Hello"+getName());

    return "success";

    }

    }

    步骤4:创建index.jsp页面

     <div>

    <h1>

    <!--显示Struts Action中message的属性内容-->

    <s:property value="message"/>

    </h1>

    </div>

    <div>

    <form action="helloWorld.action" method="post"

    请输入您的姓名:

    <input name="name" type="text" />

    <input type="submit" value="提交" />

    </form>

    </div>

    步骤5:通过浏览器访问

    2.登录案例强化:关于自动装配问题

       在开发中,通常会以JavaBean方式保存数据。所以可以有如下写法

    Action

    Jsp页面

    3.Struts2 访问servlet API

    servlet中可以通过servlet API来获取Session,在Struts中如何获取Session呢?

        解析:将用户名放入session 两种方案

    1. Servlet API解耦的访问方式

        --->01.使用ActionContext类获取ServletAPI对象对应的Map对象

        --->02.Struts2Action注入ServletAPI对象对应的Map对象

    2. Servlet API耦合的访问方式

    解耦方式:

    方案一: Servlet API进行封装   ,借助ActionContext

    提供了三个Map对象访问requestsessionapplication作用域

    通过ActionContext类获取这三个Map对象

    Object get("request")

    Map getSession()

    Map getApplication()

             案例:登录成功后,记录用户名到Session中,登录失败,跳会登录页面

             步骤一:书写Action

     

         方案二:向Action中注入ServletAPI对象对应的Map对象

           步骤一:书写Action

        

       耦合方式:

          方式一:通过ActionContext的子类ServletActionContext实现

     

    方式二:向Action实例注入Servlet API对象

     

    02.Struts中如何保存登陆用户名 ,用Session机制

    解析:在Servlet中如何获取到Session对象?

          ----->request.getSession()

    Struts中如何获取session对象

     --->ActionContext

    Struts中通过注入,核心接口 SessionAwaresetSession(Map<String,Object> map);

  • 相关阅读:
    Tableau(数据抽取)
    Oracle
    Visual Studio 2015与C#6.0新特性
    .net/c# memcached 安装和基本使用
    .net/c# memcached 获取指定前缀缓存键(keys)
    【笔记】Head First 设计模式
    C# WinForm 导出导入Excel/Doc [使用Aspose.Cells.dll]
    【转】《WCF服务编程 第一章 WCF基础》第一回
    WCF 部署到IIS(最基本的配置)
    串口通信(基础)
  • 原文地址:https://www.cnblogs.com/dongyuhan/p/7709976.html
Copyright © 2011-2022 走看看