Struts Getting Started LLDWOLF [Original]

zhaozj2021-02-16  95

Need struts package

After downloading the Struts compressed package from the Internet, unopened, copy all the .jar files in the lib subdirectory 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

Just use Struts to build J2EE architecture, Web.xml's writing is similar, I don't have to use Struts label libraries, only the most basic features, Writings are as follows:

struts example application < servlet-name> action org.apache.struts.Action.ActionServlet config /Web-inf/struts-config.xml 1 action *. Do index.jsp

That is to say, all requests for .do will be sent to org.apache.struts.Action.ActionServlet, which 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, and then gradually, and the code is as follows:

In the following steps we can see, we are as 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.

The simplest page jump

That is, 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:

click here to jump to other pages

Then add the following tags in the tag in Struts-Config.xml:

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.

Use servlet

Let's implement a simple sermlet, 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 tags in the defined for Struts-Config.xml are as follows:

Where the TYPE attribute indicates the servlet to be used, Scopen represents a valid range, which is worth noting that the sub-tag, where the Name property "Return" and the parameters of the last mapping.findForward () of the servlet are consistent.

Finally, in Index.JSP,

Click here to use servlet

Use database

We can specify database connections directly in in Struts-Config.xml, the way is as follows:

< Set-protety proty = "password" value = "" /> < / Data-Source>

In this example I use the JDBC-ODBC database connection, you need to build an ODBC connection called TEST in advance. Then you can get the database connection in the servlet:

Javax.sql.datasource DataSource = getDataSource (Request); java.sql.connection conn = DataSource.getConnection (); You can define multiple database connections in , for example:

... Properties as before ... ... Properties as before ... / Data-Source> ...

Then get the database connection in the following manner:

DataSourceA = getDataSource (Request, "A"); DataSourceB = getDataSource (Request, "B");

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

New Post(0)