struts2基础复习

这篇博客详细介绍了如何使用Struts2搭建第一个Web项目,包括创建Web项目、导入必要的jar包、配置web.xml、编写Action类以及在struts.xml中配置Action。通过遵循这些步骤,开发者可以理解Struts2的基础操作,如Action的写法与配置,并能够成功部署和访问Action来跳转到指定页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts2的第一个例子

1、 创建一个web project

2、 导入jar

3、 编写web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

    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_3_0.xsd">

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

</web-app>

4、 写一个action

5、classpath下建立struts.xml文件,把action配置进去

6、部署,打开服务器,访问action根据result标签找到要转向的页面


Action的写法与配置

继承ActionSupport类,并利用通配符配置struts.xml文件中的action

例如:

public class HelloWorldAction extends ActionSupport {

public String test(){

return "index";

}

}

在struts.xml中为

<action name="helloWorldAction_*" method="{1}" class="com.xgss.action.HelloWorldAction">

<result name="index">index.jsp</result>

</action>

 

部署访问http://localhost:8080/myStruts2/helloworldAction_test.action就可以访问到index.jsp




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值