struts1简单入门使用实例

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





猜你喜欢

转载自blog.csdn.net/haoshaoxing/article/details/46343087