Struts Getting Started LLDWOLF [Original]

zhaozj2021-02-16  140

Struts Getting Started Struts Learning Notes

The required Struts package After downloading the Struts compressed package, unopened, copy all the .jar files in the lib sub-directory to the LIB directory of your application. Of course, maybe some can not be used, but it will not be wrong. Web.xml configuration file As long as Struts is used to build J2EE architecture, the WEB.XML is similar, I don't have to use Struts label libraries, only the most basic function, the Writings are as follows: struts example application action org.apache.struts.Action.ActionServlet config / web-inf / struts- Config.xml 1 Action *. do index.jsp that is, all requests for .do suffix will be sent to org.apache.struts.Action . SectionsRVlet, this class is included in Struts.jar. As for /Web-inf/struts-config.xml, this is the configuration file used by Struts, and the file name, and path can also be customized by the user. In fact, this is the default configuration of Struts, that is, if this parameter is omitted, Struts will automatically Use /web-inf/struts-config.xml as a configuration file, or you can specify multiple profiles, different profiles are comma, "separated.

Struts-config.xml We are simply difficult, first do an empty struts-config.xml, then gradually go to the Riga function, the code is as follows: In the following steps we can see, we add content to this XML file. In fact, empty files are even nor, but the most basic label in this struts-config.xml, so I will add it first. To achieve the simplest page jump, it is to say that we have to achieve such actions, it just simply jumping from a page to another page, the function is exactly the same as the static link in the HTML page. Let's create such a link in Index.jsp: Skip to another page then in the tab in struts-config.xml Add to the Tag: OK, this simplest function is implemented. Where the Path property indicates the link request from the client, note that there is no .do suffix here. Forward is naturally the target page of jump.

Using Servlet We first implemented a simple servlet, it just returns a string "Hello, I com back!" To the client, its code is as follows: //useservlet.java import javax.servlet.http.httpservletRequest; import javax. servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; public class UseServlet extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {request.setAttribute ( "test_value", "Hello, I come back!"); return mapping.findForward ( "return ");}} The servlet must be the subclass of org.apache.struts.Action.Action and overload the execute method, and the execute method returns an org.apache.struts.Action.ActionForward class for returning the corresponding page. In this example we use mapping.forward ("return") to return this object, as for string "return", you can see that you can see that we define one of the Struts-Config.xml. Then, the corresponding label defines the of Struts-Config.xml is as follows: where the TYPE attribute indicates the servlet to be used, Scopen means a valid range, worth noting is the sub-tag, where the Name property" Return "and servlet last mapping.FindForward () The parameters are consistent.

转载请注明原文地址:https://www.9cbs.com/read-8046.html

New Post(0)