模拟struts2工作helloWorld

本篇文章是笔者在工作之余梳理关于struts2框架的知识内容,以便日后自己回顾以及记录一下自己对框架本身开始的认识。文中若有错误之处,望请指点迷津。

Struts2是一个基于MVC设计模式的Web应用框架,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。

模拟struts2工作helloWorld
1.1 struts2配置文件
新建一个普通的XML文件,struts2.xml(可任意取名):

<?xml version="1.0" encoding="UTF-8"?>
<package>
    <action name="helloRequest" 
            class="yang.zhiran.test1.helloAction"
            method="helloMethod">
        <result name="ok"  type="dispatcher">
             /test1/ok.jsp
        </result>
    </action>
</package>

1.2 控制器创建处理类

 在struts2.xml配置文件中,
 action标签内的class属性指定处理类的全名路径,
 action标签内的mehtod属性指定处理类中的具体方法名,
 result标签内的name属性指定根据返回值做出相应的处理。

创建helloAction.java,并在helloAction中新建helloMethod方法,方法返回ok。

package yang.zhiran.test1;
public class helloAction {
    public helloAction(){}
    public String helloMethod(){
        return "ok";
    }
}

1.3 过滤器
创建myFilter过滤器拦截浏览器请求路径识别请求

public class myFilter implements Filter {
    public ConfigBean config;
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)arg0;
        HttpServletResponse response=(HttpServletResponse)arg1;

        String requestPath=request.getServletPath();

        int index=requestPath.lastIndexOf(".");
        String actionName=requestPath.substring(1, index);
        if(config!=null){
            if(config.getActionName().equals(actionName)){
                try {
                    Class clazz=Class.forName(config.getActionClass());
                    Object instance=clazz.newInstance();
                    Method method=clazz.getMethod(config.getActionMethod(), null);
                    Object ReturnValue=method.invoke(instance, null);
                    if(config.getResultName().equals(ReturnValue)){
                        if(config.getResultType().equals("dispatcher")){
                            request.getRequestDispatcher(config.getResultContent()).forward(request, response);
                        }
                        else if(config.getResultType().equals("redirect")){
                            response.sendRedirect(request.getContextPath()+config.getResultContent());
                        }
                        else{
                            response.sendError(HttpServletResponse.SC_NOT_FOUND);
                        }
                    }
                    else{
                        response.sendError(HttpServletResponse.SC_NOT_FOUND);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else{

            }
        }
        System.out.println(actionName);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

        try {
            SAXReader reader=new SAXReader();
            InputStream stream=this.getClass().getClassLoader().getResourceAsStream("yang/zhiran/test1/Struct2.xml");
            ConfigBean config=new ConfigBean();
            //获取配置文档信息
            Document doc=reader.read(stream);
            Element actionElement=(Element)doc.selectSingleNode("//action");
            String actionName=actionElement.attributeValue("name");
            String actionClass=actionElement.attributeValue("class");
            String actionMethod=actionElement.attributeValue("method");

            Element resultElement=actionElement.element("result");
            String resultName=resultElement.attributeValue("name");
            String resultType=resultElement.attributeValue("type");
            String resultContent=resultElement.getTextTrim();
            //为配置对象赋值
            config.setActionName(actionName);
            config.setActionClass(actionClass);
            config.setActionMethod(actionMethod);
            config.setResultName(resultName);
            config.setResultType(resultType);
            config.setResultContent(resultContent);
            this.config=config;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

配置web.xml文件:

<filter>
    <filter-name>
        myFilter
    </filter-name>
    <filter-class>
        yang.zhiran.test1.MyFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>
        myFilter
    </filter-name>
    <url-pattern>
        /*
    </url-pattern>
</filter-mapping>

1.4配置ConfigBean对象

public class ConfigBean {
    public ConfigBean(){}

    private String actionName;
    private String actionClass;
    private String actionMethod;
    private String resultName;
    private String resultType;
    private String resultContent;
    public String getActionName() {
        return actionName;
    }
    public void setActionName(String actionName) {
        this.actionName = actionName;
    }
    public String getActionClass() {
        return actionClass;
    }
    public void setActionClass(String actionClass) {
        this.actionClass = actionClass;
    }
    public String getActionMethod() {
        return actionMethod;
    }
    public void setActionMethod(String actionMethod) {
        this.actionMethod = actionMethod;
    }
    public String getResultName() {
        return resultName;
    }
    public void setResultName(String resultName) {
        this.resultName = resultName;
    }
    public String getResultType() {
        return resultType;
    }
    public void setResultType(String resultType) {
        this.resultType = resultType;
    }
    public String getResultContent() {
        return resultContent;
    }
    public void setResultContent(String resultContent) {
        this.resultContent = resultContent;
    }
}

1.5 在webRoot下创建ok.jsp

这里写图片描述

1.6 运行测试
在浏览器中输入”localhost:8080/mystruts2/helloRequest.action”
注意:运行时要将上面的mystruts2替换成你们自己的项目名称.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值