struts1簡單入門使用例項
阿新 • • 發佈:2018-11-09
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配置
4、編寫action<?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>
建立一個類,繼承Action類,重寫 execute方法,程式碼如下:
5、啟動工程訪問 hello.action即可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"); } }