Struts记录
前段控制器ActionServlet
在Web.xml中加以定义
<!-- Standard Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
package onlyfun.caterpillar;import java.util.*;import javax.servlet.http.*;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class HelloAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter("user"); Map model = new HashMap(); if(username != null) { model.put("username", username); } else { model.put("username", "nobody"); } request.setAttribute("userInfo", model); return mapping.findForward("helloUser"); }}
<action-mappings> <action path="/hello" type="onlyfun.caterpillar.HelloAction"> <forward name="helloUser" path="/WEB-INF/jsp/hello.jsp"/> </action> </action-mappings>
<%@page contentType="text/html"%><%@page pageEncoding="UTF-8"%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts Demo</title> </head> <body> <H1>Hello, ${userInfo["username"]} !</H1> </body></html>