struct2

 

 

1.第一个Struts2入门案例
   1.找jar包
   Struts2-core
   xwork-core
   ognl
   javasist
   freemarker
   commons-lang
   commons-io
   commons-fileupload 

   2.在web.xml文件中植入配置,配置了核心过滤器(XXXXFilter)
      核心控制器
      核心过滤器的名称:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

     Ctrl+Shift+T

   3.在src下创建一个struts.xml文件
   <package name="default" namespace="/" extends="struts-default">
      <action name="xxAction逻辑名称:URL请求书写的名称" class="类的物理地址">
        <result name="success">/index.jsp</result>
         <result name="login">/login.jsp</result>
      </action>
   </package>
   4.在action包中创建一个Action类
     实现了特定接口的一个类 Action接口
     public class LoginAction implements Action{
       public String execute(){
         return "success";
       }

     }
     5.写一个index.jsp页面

2.实现登录:认证自动装配
 初级的自动装配
    page:name  Action: name保持一致

 中级的自动配置
    page:user.namee  Action :user对象

  高级的自动配置
    page:name       Action:user(多实现了一个接口:ModelDriven)
                     前提条件:要手动new出user

3.保存用户信息:ServletAPI如何使用?在Action中如何获取到session对象
  解耦
    方式一:ActionContext.getContext().getSession()
    方式二:IOC注入
           实现了接口:SessionAware:  核心方法 setSession(Map<String,Object> map)
            Map<String,Object> map;
            形成get和set方法

            Action的execute方法中,使用map.put()
  耦合
     方式一:ServletActionContext.getRequest().getSession();


     方式二:IOC 注入
         实现一个接口:ServletRequestAware       setServletRequest(HttpServletRequest request)
        Action 中公开一个变量:HttpServletRequest request; get和set方法

        Action中的execute方法中就可以 HttpSession=request.getSession();

4.数据校验
5.Struts2常用标签
-------------------2016年9月25日09:14:36
1.Struts2和MVC
 解析:MVC是一种模式,设计思想
     Struts2是这种模式的一种实现
     Spring MVC
2.WebWork和Struts2关系
 解析:Struts1   官方提供的框架
      WebWork  

3.Struts2标签
 1.导入指令
  <%@ taglib uri="/struts-tags" prefix="s"%>

 2.使用
 <s:form>
   
 </s:form>

4.自动装配autoaware
  1.login.jsp
    name="username"

  2.Action中的成员变量名称和登录界面中name属性值一致

5.一个异常
Stacktraces (栈跟踪)
java.lang.NullPointerException 空指针异常

    cn.happy.action.LoginAction.execute(LoginAction.java:12)

6.username password  ------->user
 

  jsp页面
  <input name="user.username"/>
  <input name="user.password"/>

7.洋洋说
  洋洋说,这么写太恶心,能不能界面上还是username,也能实现自动装配呢?

   解析:ModelDriven (模型驱动)
    public abstract interface com.opensymphony.xwork2.ModelDriven {
      public abstract java.lang.Object getModel();
    }
  小tip:
  1.让Action实现一个ModelDriven<UserInfo>
  2.user对象手动实例化
  3.UserInfo getModel(){
     return user;
  }

 8.Servlet API
 解析:API(Application Programming Interface) 应用程序编程接口
     就是和Servlet相关的一组接口和类

     Servlet API 文档
     接口和类以及方法的说明,描述
     xxx.chm 
 9.我想在登录成功后,拿到Session,给Session保存用户名。

  1.如何在Action中获取到session对象
  2.将用户名放入session作用域

 10.获取session的两种方案
    方式一:耦合
   
HttpSession session2 = ServletActionContext.getRequest().getSession();
 
    方式二:解耦
 Map<String, Object> session = ActionContext.getContext().getSession();

   通过注入方案:IOC

有待考究:blogs一小篇
Map<String, Object> session = ServletActionContext.getContext().getSession();

  作业:1.有待考究:blogs一小篇
       2.所有案例形成blogs
       3.画图和获取session四种方案
       4.下午:上机练习和课后题
      
 第三章 Struts2配置详解
 2016-9-26 08:18:01
 1.Struts2标签
 2.核心控制器 web.xml
 3.Action
  让一个普通类变成Action
   方式一:实现 Action接口
      必须程序员手动的重写execute()

   方式二:继承 ActionSupport
       不用程序员手动重写execute()
          因为在ActionSupport对execute()做了默认实现
        要想启用验证,必须实现该类,或者说该类的子类

 4.Result
   
5.Struts.xml
  <constant name="struts.devMode" value="true" />   改动不重启服务
 i18n  (国际化)
 internationalization
 Internationalization
 
 6.如何搜索xml文件加载顺序
   1.找到项目的web.xml找到核心过滤器
   2.找到init方法
      StrutsPrepareAndExecuteFilter类中

     public void init(FilterConfig filterConfig) throws ServletException {

            dispatcher = init.initDispatcher(config);   //呵呵   
            postInit(dispatcher, filterConfig);
      
    }
     InitOperations类

      public Dispatcher initDispatcher( HostConfig filterConfig ) {
        Dispatcher dispatcher = createDispatcher(filterConfig);
        dispatcher.init();  //呵呵
        return dispatcher;
    }

    Dispatcher类
      public void init() {
            init_FileManager();
            init_DefaultProperties(); // [1]
            init_TraditionalXmlConfigurations(); // [2]   嘻嘻
            init_LegacyStrutsProperties(); // [3]
            init_CustomConfigurationProviders(); // [5]
            init_FilterInitParameters() ; // [6]
            init_AliasStandardObjects() ; // [7]   
    }
     接下来去找
     private void init_TraditionalXmlConfigurations() {
        String configPaths = initParams.get("config");
        if (configPaths == null) {
            configPaths = DEFAULT_CONFIGURATION_PATHS;  //呵呵
        }
      
    }
    /**
     * Provide list of default configuration files.
     */
    private static final String
    DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";

 

7.完整顺序
default.properties文件 
struts-default.xml 
struts-plugin.xml 
struts.xml 
struts.properties 
web.xml


Action是数据中转站?
解析: Action将登录框中用户名A 转给 欢迎界面B
方式二:Model的数据给View
8.动态方法调用
DMI Dynamic Method Invoke
SUCCESS "succss"
LOGIN login
ERROR error
INPUT input
NONE none

普通的请求方式
<action method="list">
<action method="add">
<action method="edit">
<action method="del">

/***
* 图书列表
*/
public String list() throws Exception {
System.out.println("DMIAction========list");
return "list";
}
/**
* 添加图书
*/
public String add() throws Exception {
System.out.println("DMIAction========add");
return "add";
}
/**
* 修改图书
*/
public String edit() throws Exception {
System.out.println("DMIAction========edit");
return "edit";
}

/**
* 删除
*/

public String del() throws Exception {
System.out.println("DMIAction========del");
return "del";
}


启用DMI,先抉条件
<!-- 开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

在浏览器端 action逻辑名称!方法
不用,安全性低


9.通配符

Book
-----添加图书
------修改图书

User
---添加用户
---修改用户
10.ServletAPI
请求流程图
*Struts2 UI标签
*配置文件 中常量国际化的设置,推导出所有的常量在default.properties中进行查找
*配置文件 struts-default.xml strtus-plugin.xml struts.xml
*6个配置文件加载顺序
*ActionSupport
*一个action出现CRUD,如何配置
*太浪费Action节点 DMI
*通配符 *_*
面试Struts2 必须面试第四章

第四章 Struts2深入
2016年9月28日08:29:07
1.拦截器
2.文件上传和下载 光文杰
3.数据校验 汪静
4.国际化 张振
5.类型转换 张升平
6.jq ui 黄荣
easy ui 景佩佩
7.自定义mvc 迟明洋
xml定义
dom4j
反射

 

1.拦截器
2.Struts执行流程
2.1 web.xml中 StrutsPrepareAndExecuteFilter类

2.2 找到doFilter方法
体现出一种设计模式
解释了Struts2中 request 并不是HttpServletRequest
request = prepare.wrap(包装)Request(request);

***:ActionMapping mapping = prepare.findActionMapping(request, response, true);

findActionMapping方法:
public ActionMapping findActionMapping() {
//先看内存中有没有映射结果!
ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY);

//没有,找到ActionMapper构建
if (mapping == null || forceLookup) {

mapping = dispatcher.getContainer().getInstance(ActionMapper.class).
}

return mapping;
}

2.3 上面代码已经保证mapping不是null
if (mapping == null) {

} else {
//code execute
execute.executeAction(request, response, mapping);
}

executeAction方法原型如下:
public void executeAction() throws ServletException {
dispatcher.serviceAction(request, response, mapping);
}

*serviceAction原型
public void serviceAction(){

ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);

}

*:寻找ActionInvocation

if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation():返回值类型是ActionInvocation);
} else {
proxy.execute();
}

 

posted @ 2016-10-14 17:12  吴玄坤  阅读(308)  评论(0编辑  收藏  举报