struct2面试准备

二 工作流程
1、客户端浏览器发出HTTP请求.
2、根据web.xml配置,该请求被FilterDispatcher接收
3、根据struts.xml配置,找到需要调用的Action类和方法, 并通过IoC方式,将值注入给Aciton
4、Action调用业务逻辑组件处理业务逻辑,这一步包含表单验证。
5、Action执行完毕,根据struts.xml中的配置找到对应的返回结果result,并跳转到相应页面
6、返回HTTP响应到客户端浏览器
看到网友的对Struts2的原理总结,我自己也总结以便后续的面试,以下是我的疑问

下面我们看下,需要的jar包

  前面两个是apache commons的jar包,暂且忽略

  freemarker提供了另一种展现方式

  ognl提供了OGNL表达式

  struts2-core提供struts2核心包

  xwork-core由于struts2很多事基于webwork的,因此也需要这个的核心包

  我们提供了三个jsp

登陆界面login.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
    <s:textfield name="username" key="user"/>
    <s:textfield name="password" key="pass"/>
    <s:submit key="login"/>
</s:form>
</body>
</html>

登陆成功界面welcome.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title><s:text name="succPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
    <s:text name="succTip">
        <s:param>${sessionScope.user}</s:param>
    </s:text><br/>
</body>
</html>

当login.jsp触发action时,就会向后抬发送login.action的请求,这个请求被后台拦截,交给struts.xml中配置的action处理

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <!-- 指定全局国际化资源文件 -->
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <!-- 指定国际化编码所使用的字符集 -->    
    <constant name="struts.i18n.encoding" value="GBK"/>
    <!-- 所有的Action定义都应该放在package下 -->
    <package name="test" extends="struts-default">
        <action name="login" class="com.test.action.LoginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
</struts>
下面是LoginAction的代码,可以看到成功登陆后,程序把username写入session中。以便于我们在welcome.jsp中利用${sessionScope.user}取得名字
package com.test.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String execute() throws Exception {
        if (getUsername().equals("xingoo") && getPassword().equals("123")) {
            ActionContext.getContext().getSession().put("user", getUsername());
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
}

  登陆失败

注意点二:

1)Struts2中的Action是线程安全的吗?

2)Servlet是线程安全的吗?

3)一个Action只能处理一个请求吗?

1)Struts2中的Action是线程安全的,每个请求都会创建单独的Action类来处理请求。

2)Servlet并不是线程安全的,是单例模式开发,如果有共享变量,要注意线程安全。

3)一个Action是可以处理多个请求的,通过配置不同的method和创建不同的method

ActionContext、ServletContext、pageContext的区别?
1)ActionContext是当前的Action的上下文环境,通过ActionContext可以获取到request、session、ServletContext等与Action有关的对象的引用;

2)ServletContext是域对象,一个web应用中只有一个ServletContext,生命周期伴随整个web应用;

3)pageContext是JSP中的最重要的一个内置对象,可以通过pageContext获取其他域对象的应用,同时它是一个域对象,作用范围只针对当前页面,当前页面结束时,pageContext销毁,

生命周期是JSP四个域对象中最小的。

Spring mvc 和 struct2的却别:

  • 拦截的级别不一样
Struts2是类级别的拦截, 一个类对应一个request上下文。
SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应。
所以说从架构本身上SpringMVC就容易实现restful url,而struts2的架构实现起来要费劲,因为Struts2中Action的一个方法可以对应一个url,而其类属性却被所有方法共享,这也就无法用注解或其他方式标识其所属方法了。
  • 方法的独立性,资源共享方式不一样 
  SpringMVC的方法之间基本上独立的,独享request response数据,请求数据通过参数获取,处理结果通过ModelMap交回给框架,方法之间不共享变量,
  Struts2搞的就比较乱,虽然方法之间也是独立的,但其所有Action变量是共享的,这不会影响程序运行,却给我们编码 读程序时带来麻烦,每次来了请求就创建一个Action,一个Action对象对应一个request上下文。
  • Struts2原则上比springMVC更耗内存。
  因为Struts2需要针对每个request进行封装,把request,session等servlet生命周期的变量封装成一个一个Map,供给每个Action使用,并保证线程安全。
  • 拦截器实现机制不一样
Struts2有以自己的interceptor机制,SpringMVC用的是独立的AOP方式,所以,这样导致了Struts2的配置文件量还是比SpringMVC大。
  • 入口不一样
  SpringMVC的入口是servlet,而Struts2是filter(这里要指出,filter和servlet是不同的。以前认为filter是servlet的一种特殊),这就导致了二者的机制不同,这里就牵涉到servlet和filter的区别了。
  • 对ajax的集成度不一样
SpringMVC集成了Ajax,使用非常方便,只需一个注解@ResponseBody就可以实现,然后直接返回响应文本即可,而Struts2拦截器集成了Ajax,在Action中处理时一般必须安装插件或者自己写代码集成进去,使用起来也相对不方便。
  • 数据验证方式不一样
SpringMVC验证支持JSR303,处理起来相对更加灵活方便,而Struts2验证比较繁琐,感觉太烦乱。
  • 与spring的衔接度不一样
 spring MVC和Spring是无缝的。从这个项目的管理和安全上也比Struts2高(当然Struts2也可以通过不同的目录结构和相关配置做到SpringMVC一样的效果,但是需要xml配置的地方不少)。
  • 设计思想不一样
设计思想上,Struts2更加符合OOP的编程思想, SpringMVC就比较谨慎,在servlet上扩展。
  • 开发效率和性能不一样
SpringMVC开发效率和性能远高于Struts2。
  • 配置度不一样
SpringMVC可以认为已经100%零配置
原文地址:https://www.cnblogs.com/kebibuluan/p/7799875.html