struts2之基本配置

1.在WEB-INF/lib加上10个lib包

commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
xwork-core-2.3.15.3.jar
2.在/src文件夹下创建struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>


3.在web.xml配置触发器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name></display-name>
    <!-- Struts2的Filter,所有请求都被映射到Struts2上 -->
    <filter>
        <filter-name>struts</filter-name><!-- filter名称 -->
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<!-- filter入口 -->
        </filter-class>
    </filter>
    <!-- struts2的filter的uri配置 -->
    <filter-mapping>
        <filter-name>struts</filter-name><!-- filter名称 -->
        <url-pattern>/*</url-pattern><!-- 截获所有URL -->
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

4.在index.jsp创建表单
4.1.在.jsp加上以下语句才能用struts的相关标签

<%@taglib prefix="struts" uri="/struts-tags"%>

4.2.表单内容

    <struts:form action="loginPerson">
        <struts:label value="登陆系统"></struts:label>
        <!-- 文字标签 -->
        <struts:textfield name="account" label="帐号"></struts:textfield>
        <struts:password name="password" label="密码"></struts:password>
        <struts:submit value="登陆"></struts:submit>
        <!-- 提交按钮显示的文本由value决定 -->
    </struts:form>


5.完成对 action="loginPerson"的交代
5.1在包com.test.action下新建LoginAction(继承ActionSupport),实现excute方法

    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return SUCCESS;
    }

SUCCESS代表回应的内容
如果有用到表单的变量,一定要有get和set方法
5.2在struts.xml添加action的内容:
在struts标签之间加入

    <!-- 定义一个package -->
    <package name="main" extends="struts-default">

        <!-- 所有的全局results, global-results要在最上面-->
        <global-results>
        <!-- 名为login的result -->
            <result name="login">/index.jsp</result>
        </global-results>
        <action name="loginPerson" class="com.test.action.LoginAction">
            <!-- 在此定义的result仅在loginPerson这个action中有效 -->
            <!-- 否则需要定义global-results -->
            <result name="success">/result.jsp</result>
        </action>

    </package>

action name="loginPerson"这行声明了loginPerson这个action的代码文件
result name="success"这行声明了success这个变量作为返回值对应的内容

Done!

原文地址:https://www.cnblogs.com/xingyyy/p/3430349.html