Struts2开发程序步骤

版权声明:@lingtouyang1997 https://blog.csdn.net/weixin_43209201/article/details/86614261

Struts2开发程序步骤

1、新建Dynamic web 项目

关于web项目创建后WEB-INF下面没有出现web.xml的解决方法:
https://blog.csdn.net/weixin_43209201/article/details/86607913

2、导入Struts2需要的jar包

如何快速找到Struts2所需要的基本jar包:
https://blog.csdn.net/weixin_43209201/article/details/86544691

3、配置web.xml文件,添加Struts2核心过滤器

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	version="4.0">
	<display-name>Struts2_ExcelOperation_Demo</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<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>
	<!-- 作用:接管请求。将请求交给struts2来处理 -->
<!-- 注意:不同版本的核心过滤器不一样
            2.1以前是org.apache.struts2.dispatcher.FilterDispatcher
            2.1以后org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
-->

</web-app>

官方web.xml参考网址:https://struts.apache.org/core-developers/web-xml.html

4、添加index.jsp页面

5、新建一个Action类(用于处理请求)

6、配置struts.xml文件(该配置文件位于src下,文件名不能被更改

<struts>
<!-- 
        constant 常量配置
        struts.devMode 设置是否是开发模式
     -->
    <constant name="struts.devMode" value="true" />
    <!-- 设置编码 -->
    <constant name="struts.i18n.encoding" value="utf-8"/>
    <!-- 设置action的扩展名 -->
    <constant name="struts.action.extension" value="action,,do"/>
    <!-- 
        package表示一个包每个包下可以配置多个action  包的作用主要用来区分不同模块的配置
        不同模块配置在不同的包下
        name 是包名  在项目下唯一 通常使用模块名做为包名
        extends 表示继承 必须直接或者间接继承struts-default
        namespace 表示命名空间  命名空间和请求名直接相关  
        请求名=项目发布路径名+namespace名+action名
        namespace名称通常也和模块名相关
     -->
    <package name="default" extends="struts-default" namespace="/user">
        <!-- action 配置请求名 一个action表示对一个请求的处理
            name在同一个包下唯一 会作为请求名称 该名称会自动将请求的扩展名去掉,所以不用配置扩展名
            class 配置处理请求的类的完全限定名=包名+类名。如果不配置class默认由
            com.opensymphony.xwork2.ActionSupport来处理。
            method 配置处理方法的名称  默认由execute方法处理
            在struts2中处理方法  必须是 public的,返回值必须是String。
         -->
        <action name="hello" class="cn.sxt.action.HelloAction" method="hello">
            <!-- result配置的结果集处理  每个action中可以及配置多个result 
                name表示结果集名称 和处理方法的返回值进行匹配 默认是success;
                type表示结果集类型
                    dispatcher 转发-默认
                    redirect 重定向
                    redirectAction 重定向到另外一个action
                    stream 流
                    chain  表示action链
                result中填写要跳转的页面的路径
            -->
            <result name="success" type="dispatcher">/index.jsp</result>
        </action>
    </package>
    <!-- 
        include用于添加其他配置文件,在团队开发中使用
        不同模块一般使用不同的配置文件,在总的配置文件中加入即可
     -->
    <include file="user.xml"></include>
</struts>

猜你喜欢

转载自blog.csdn.net/weixin_43209201/article/details/86614261