• Struts2的配置与简单案例


    1. 创建一个dynamic web project(创建时让它自动生成web.xml文件)


    2.引入相关jar包


    3.在web.xml中进行配置
     (启动tomcat服务器之后第一个加载的文件就是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>
    <url-pattern>/*</url-pattern>
    </filter-mapping>


    4.创建struts的核心文件(struts.xml),将其创建在Java Resources-src文件目录下,内容为:


    <?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>


    </struts>

    5.同样在Java Resources-src文件目录下创建一个Action类,继承自ActionSupport,并覆盖父类中的execute方法:


    public class HelloWorldAction extends ActionSupport {

    @Override
    public String execute() throws Exception {
    System.out.println("执行Action");
    return SUCCESS;
    }

    }


    6.在struts.xml中的<struts>标签中添加:


    <struts>
    <package name="default" namespace="/" extends="struts-default">
    <action name="helloworld" class="default package.HelloWorldAction">
    <result>/result.jsp</result>
    </action>
    </package>
    </struts>

    7.创建视图(在WebRoot目录下创建result.jsp):


    <body>
    This is result.jsp!
    </body>

    8.调试运行

  • 相关阅读:
    78. Subsets java solutions
    77. Combinations java solutions
    236. Lowest Common Ancestor of a Binary Tree java solutions
    86. Partition List java solutions
    39. Combination Sum java solutions
    129. Sum Root to Leaf Numbers java solutions
    杭电1004
    杭电1003
    杭电1002
    杭电1001
  • 原文地址:https://www.cnblogs.com/xingyazhao/p/6704922.html
Copyright © 2020-2023  润新知