• Struts2之2.5.10配置


      struts2开发环境搭建,环境eclipse + struts 2.5.10 + Tomcat 9

    1、eclipse下新建web工程

    2、将jar包拷贝到WEB-INF/lib下

    3、配置struts.xml 和web.xml【这两个文件可以在下载struts2时struts-2.5.10.1-apps中得到,解压这里面的war包在相应位置能找到,包括这里没有写得log4j2.xml】

    struts.xml

    <?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">
            <action name="helloWorld" class="com.marost.action.HelloWorldAction" method="excute">
                <result name="success">/WEB-INF/page/hello.jsp</result>
            </action>
        </package>
    </struts>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app id="starter" version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
      <display-name>Struts 2 Rest Example</display-name>
    
    
      <!-- Filters -->
      <!-- START SNIPPET: filter -->
        <filter>
            <filter-name>action2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <!-- END SNIPPET: filter -->
    
        <filter-mapping>
            <filter-name>action2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- Welcome file lists -->
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
        <!-- Restricts access to pure JSP files - access available only via Struts action -->
        <security-constraint>
            <display-name>No direct JSP access</display-name>
            <web-resource-collection>
                <web-resource-name>No-JSP</web-resource-name>
                <url-pattern>*.jsp</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>no-users</role-name>
            </auth-constraint>
        </security-constraint>
    
        <security-role>
            <description>Don't assign users to this role</description>
            <role-name>no-users</role-name>
        </security-role>
    
    </web-app>

    4、新建action

    package com.marost.action;
    
    public class HelloWorldAction {
        private String msg;
        
        public String getMessage() {
            return msg;
        }
        public String excute(){
            msg = "你好!";
            return "success";
        }
    }

    5、新建jsp

    在body中输出action中的内容

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ${message }
    </body>
    </html>

    最终的工程结构如下:

    访问:http://localhost:8080/Struts2/helloWorld

  • 相关阅读:
    iframe如何刷新的三种解决方案
    检查Windows服务是否启动
    关于 wcf 共享端口portSharing
    关于 wcf 异常处理——页面超时
    关于ASP.NET MVC4 Web API简单总结
    关于WCF访问跨越站点获取文件路径的AppDomain方法区别与HttpContext
    DataTable、实体类Model和泛型List之间的转化
    异步load()加载 ckeditor 编辑器
    如何在Eclipse中自动添加注释
    [转]eclipse中编写代码时如何自动提示变量名?
  • 原文地址:https://www.cnblogs.com/marost/p/6701805.html
Copyright © 2020-2023  润新知