struts2框架快速搭建

1.导包

使用maven

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.24</version>
</dependency>

2.准备action类

public class HelloAction {
    public String hello(){
        System.out.println("hello struts2");
        return "success";
    }
}

3.书写核心配置文件 struts.xml  放在resources里

<package name="hello" namespace="/" extends="struts-default">
    <action name="helloAction" class="com.hd.action.HelloAction" method="hello">
        <result name="success">/hello.jsp</result>
    </action>
</package>

核心配置文件详解

package

<!--
name:包的名字  随意起  但是与其他包的名字不能重复
namespace:命名空间  访问地址的前缀
extends:继承  必须继承struts-default
abstract:抽象 等待被继承  起一个标识作用
-->
<package name="hello" namespace="/" extends="struts-default"></package>

action

<!--
name:给action起的名字  没有特殊规定可以随便起,它是外部访问路径名
     尽量和Action类的名字保持一致
class:类的完整路径名
mathod:方法名  决定着访问类中的方法
-->
<action name="helloAction" class="com.hd.action.HelloAction" method="hello">
</action>

result

<!--
name:和HelloAction里面的 返回值一样   和访问的方法的返回值保持一致
type:转发  重定向
标签中的值  要跳转的页面
-->
<result name="success">/hello.jsp</result>

include

<!--引入其他配置文件-->
<include file="struts2.xml"></include>

猜你喜欢

转载自blog.csdn.net/qq_41998581/article/details/81174351
今日推荐