struts2配置

一、先导入jar包

先解压个空白的项目,从里面复制jar包

二、导完jar包后,需要配置struts.xml文件和web.xml文件

1.导入web.xml文件,改配置.

相当于加了一个struts过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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 Blank</display-name>

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

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

  

</web-app>

 2.加入struts.xml文件到src目录下

struts.xml文件配置, 主要配置的是action

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

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
           <!-- 对应着IndexAction里面的execute方法 -->
        <action name="index" class="com.itnba.maya.controller.IndexAction">
            <!-- execute方法return SUCCESS时不用写name -->
            <result>
                index.jsp <!--跳转的页面  -->
            </result>
            
            <result name="error">
                index_error.jsp
            </result>
            
            <result name="haha">
                index_haha.jsp
            </result>
        </action>
        
        <!-- 直接跳转不用处理数据时 不需要写class -->
        <action name="home">
            <result>
                home.jsp
            </result>
        </action>
    </package>

   
</struts>

3.建一个类,继承com.opensymphony.xwork2.ActionSupport    

在该类中,重写execute()方法  

成员变量用来相互传值,生成get set

package com.itnba.maya.controller;

import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport {
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    @Override
    public String execute() throws Exception {
        msg+="haha";
        return SUCCESS;
    }
    
}

这样就简单的配置完成了,实际运用还要根据情况修改

原文地址:https://www.cnblogs.com/hq233/p/6543107.html