Struts1.1 learning notes
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:
XML Version = "1.0" Encoding = "ISO-8859-1"?>
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:
XML Version = "1.0" Encoding = "ISO-8859-1">
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
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
Where the TYPE attribute indicates the servlet to be used, Scopen represents a valid range, which is worth noting that the
Finally, in Index.JSP,
Use database
We can specify database connections directly in
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 data-sources>, for example:
Then get the database connection in the following manner:
DataSourceA = getDataSource (Request, "A"); DataSourceB = getDataSource (Request, "B");