struts1简单入门使用实例

本文详细介绍了如何配置并使用Struts框架,包括添加jar包、web.xml配置、struts-config.xml配置、编写action类及启动工程访问的步骤。

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

1、添加相关jar包 struts.jar、 commons-logging-1.1.jar 、commons-digester.jar、commons-beanutils.jar

2、web.xml配置

    <servlet>
   <servlet-name>action</servlet-name>
   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
   </servlet>

//*.action 表示所有以.do为结尾的请求都会被交给ActionServlet处理

<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping> 

3、struts-config.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
	<data-sources />
	<!-- 下面注释的为非必配标签 -->
	<!-- <form-beans>
	</form-beans>
	<global-exceptions />
	<global-forwards>
		<forward name="error.operation" path="/error/opt_error.jsp" />
		<forward name="error.system" path="/error/sys_error.jsp" />
	</global-forwards> -->
	<action-mappings>
		<action type="com.hsx.struts.action.HelloAction"  path="/hello" parameter="Action" scope="request" validate="true">
			<forward name="success" path="/index.jsp" />
		</action>
	</action-mappings>
</struts-config>
4、编写action

创建一个类,继承Action类,重写 execute方法,代码如下:

public class HelloAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
		System.out.println("hehe");
		return mapping.findForward("success");
	}
     
}
5、启动工程访问 hello.action即可





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值