Struts2代码实例

Struts2实际代码运用

(文章中的代码只是参考,并不一定能连起来运行,需要靠自己理解编写相应代码)

导入Strtus2 依赖包

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <!-- 过滤器,拦截web资源给Struts2的过滤器处理 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- /*:匹配所有url;/:不会匹配*.jsp这样结尾的url -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml 配置(一般在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>
    <!-- constant设置struts属性 -->
    <!-- 设置开发模式 -->
    <constant name="struts.devMode" value="true"/>
    <!-- 设置编码形式为GB2312 -->
    <constant name="struts.i18n.encoding" value="GB2312"/>
    <!-- 
        package: 将Action配置封装。可有多个package
        name: 给包起个名字,起到标识作用(必需)
        namespace: 给action的访问路径中定义一个命名空间,请求满足命名空间才会进入此package,默认为""
        abstract: 包是否为抽象的; 标识性属性,标识该包不能独立运行,专门被继承
        extends: 继承一个指定包
    -->
    <package name="struts2" namespace="/struts" extends="struts-default">
        <global-exception-mappings>
            <exception-mapping result="逻辑视图" exception="异常类型"/>
        </global-exception-mappings>
        <!-- 
            action: 配置action类
            name: 决定了Action访问资源名(必需)
            class: action的完整类名
            method: 指定调用Action中的哪个方法来处理请求
            converter: 指定Action使用的类型转换器
        -->
        <action name="load" class="com.struts.TestAction" method="hello" >
            <!--
                result元素:结果配置 
                name属性: 标识结果处理的名称,与action方法的返回值对应
                type属性: 指定调用哪一个result类来处理结果,默认使用转发.
            -->
            <result name="success" type="dispatcher" >/hello.jsp</result>
            <exception-mapping result="逻辑视图" exception="异常类型"/>
        </action>
    </package>
    <!-- 引入其他struts.xml配置文件 -->
    <include file="struts1.xml"/>
</struts>

action处理类:com.struts.TestAction

package com.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;public class TestAction {
    //没有指定方法struts默认执行execute()方法
    public String execute() throws Exception{
        //获取request和response
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        //设置request和response的编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("往前端写数据");
        return SUCCESS;
    }
    public String hello(){
        return "success";
    }
}
原文地址:https://www.cnblogs.com/wccw/p/12976907.html