zoukankan      html  css  js  c++  java
  • Eclipse配置Struts2.x

    问了我同学现在JavaWeb主流框架是哪些。他说基本框架是SSH,struts2+spring+hibernate,流行的是SSM,springmvc+spring+mybatis,原本计划学下Struts1框架呢,这样就不学了,先学下Struts2,首先就是eclipse配置Struts2.

    一、下载Struts2

    从http://struts.apache.org/download.cgi#struts25101下载struts2的包,这里我选的是min的下载的

    二、导入Struts2

    下载之后解压,将lib下的所有文件复制到已创建的Struts2项目中。

    三、strut2配置

    1.在src下创建struts.xml文件,在xml中做如下配置,xml里引入的<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
     "http://struts.apache.org/dtds/struts-2.5.dtd">,这个dtd是用来校验这个xml的内容格式的,如果缺少节点,会跟dtd里的东西对不上,从而报错,这里还要注意一下根节点是<struts></struts>,今天配置的时候就忘记加根结点了。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
        <package name="default" extends="struts-default">
                <default-action-ref name="hello" />
                <action name="hello">
                    <result>/hello.jsp</result>
                </action>
        </package>
    </struts>

    2.在WEB-INF/web.xml中配置如下,这里filter和filter-mapping是一一对应的,用各自的filter-name对应,我开始配置的时候定义了两个filter:struts-execute、struts-prepare但只定义了一个filter-mapping:struts-prepare,其实这两个filter的class是可以和到一起的,使用下面的org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter类。

       <filter>
            <filter-name>struts-prepare</filter-name>
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts-prepare</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    3.在WebContext下新建hello.jsp文件,因为在上面的struts.xml中配置的action的result是/hello.jsp。如果hello.jsp放在WEB-INF下,result应该是/WEB-INF/hello.jsp.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
        <package name="default" extends="struts-default">
                <default-action-ref name="hello" />
                <action name="hello">
                    <result>/WEB-INF/hello.jsp</result>
                </action>
        </package>
    </struts>

    四、运行

    在hello.jsp文件中的body中输入一行<h2>Hello,Strut2!</h2>。重启Tomcat,在浏览器输入http://localhost:8088/MyStruts2/hello。

    这里要感谢下我的同学也是我大学室友张蒙,这么晚了还帮忙解决问题。

  • 相关阅读:
    微擎开发笔记
    Array对象的方法实现(1)----Array.prototype.push和Array.prototype.concat(实现常规参数的功能)
    ThinkCMF 5 基础门户CMS框架的模板widget标签实现
    [转]mysql为什么默认有那么多root用户,还有用户名为空的用户?
    PHP 5.4特性 trait
    道破人性
    c#使用easyhook库进行API钩取
    黑马eesy_15 Vue:04.综合案例(前端Vue实现)
    黑马eesy_15 Vue:04.Vue案例(ssm环境搭建)
    黑马eesy_15 Vue:03.生命周期与ajax异步请求
  • 原文地址:https://www.cnblogs.com/5ishare/p/6601457.html
Copyright © 2011-2022 走看看