Install Tomcat plugin
Install Tomcat plugin
Note To add JDK's Tools.jar to the configuration, or you will not be able to compile JSP files.
New Tomcat project, name Struts
New Tomcat project, name Struts
Copy all files in the lib directory of jakarta-struts-1.1.zip (new version should not be no problem). Jar and .tld files are copied to struts / web-inf / lib directory.
Copy all files in the lib directory of jakarta-struts-1.1.zip (new version should not be no problem). Jar and .tld files are copied to struts / web-inf / lib directory.
Adding these JAR packages to the project via Project-> Properties-> Java Build Path
Drive to connect the MySQL database mysql-connector-java-
3.0.14-Production-bin.jar to copy to struts / web-inf / lib directory
Drive to connect the MySQL database mysql-connector-java-
3.0.14-Production-bin.jar to copy to struts / web-inf / lib directory
New web-inf / src / com / ben / test / struts directory, new Helloform.java and HelloAction.java files in the directory
New web-inf / src / com / ben / test / struts directory, new Helloform.java and HelloAction.java files in the directory
In the Struts directory, create a new JSP directory to store JSP files, create new hello.jsp and SayHello.jsp in the directory
In the Struts directory, create a new JSP directory to store JSP files, create new hello.jsp and SayHello.jsp in the directory
In the Struts / Web-INF directory, create a new struts-config.xml file, as follows:
In the Struts / Web-INF directory, create a new struts-config.xml file, as follows:
XML Version = "1.0" encoding = "UTF-8"?>
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
data-source>
data-sources>
form-beans>
action>
action-mappings>
Struts-Config>
Create a new web.xml file in the struts / web-inflicity, the content is as follows:
Create a new web.xml file in the struts / web-inflicity, the content is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
init-param>
servlet>
servlet-maping>
taglib>
taglib>
taglib>
web-app>
Note the red part, define the servlet class that receives the request is org.apache.struts.Action.ActionServlet, the servlet is responsible for reading /web-inf/struts-config.xml to determine how the user's HTTP request
Note the red part, define the servlet class that receives the request is org.apache.struts.Action.ActionServlet, the servlet is responsible for reading /web-inf/struts-config.xml to determine how the user's HTTP request
The content of Helloform.java is as follows:
The content of Helloform.java is as follows:
Package com.ben.test.struts;
Import javax.servlet.http.httpservletRequest;
Import org.apache.struts.Action.Actionerror;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.action.actionmapping;
Public Final Class Helloform Extends Actionform {
PRIVATE STRING NAME;
Public string getname () {
Return name;}
Public void setname (String name) {
THIS.NAME = Name;
}
Public ActionerRors Validate (ActionMapping Mapping, httpservletRequest Request) {
ActionerroS Errors = new actionerrors ();
// Error handle, add yourself
Return Errors;
}
}
The content of HelloAction.java is as follows:
The content of HelloAction.java is as follows:
Package com.ben.test.struts;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.action;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
Import java.sql. *;
Import java.sql. *;
Import javax.sql. *;
Public final class helloaction extends action {
Public final class helloaction extends action {
Public ActionForward Execute (ActionMapping Mapping, Actionform Form,
HTTPSERVLETREQUEST REQUEST,
HttpservletResponse response) throws exception {
Helloform Helloform = (Helloform) Form;
String name = helloform.getname ();
INT ID = 0;
DataSource DataSource = (DataSource) servlet.getServletContext (). GetAttribute (
"org.apache.struts.action.data_source");
Try {
Try {
Connection conn = DataSource.getConnection ();
Statement Stmt = conn.createstatement ();
ResultSet RS = Stmt.executeQuery ("Select * from name " ");
While (rs.next ()) {
ID = rs.getint ("id");
}
Rs.close ();
Stmt.close ();
CONN.CLOSE ();
}
Catch (SQLException E) {
Throw New Sqlexception ("Database Error");
}
Request.setttribute ("Name", New Integer (ID));
Return mapping.findforward ("Sayhello");
}
}
The code is created in accordance with the data source configured in Struts-Config, and then obtain the ID corresponding to the Name in accordance with the NameTable of the Name to the database, then let the ID value of the request to Sayello More struts-config configuration, Sayhello corresponds to this file
The content of Hello.jsp is as follows:
The content of Hello.jsp is as follows:
<% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
hEAD>
Name:
html: form>
body>
html: html>
The content of SayHello.jsp is as follows:
The content of SayHello.jsp is as follows:
<% @ page contenttype = "text / html; charset = uTF-8" Language = "java"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
hEAD>
Welcome <% = Request.getaTattribute ("name")%>
body>
html: html>
A very simple example, I hope to help everyone
A very simple example, I hope to help everyone
Zaiben @ smth, 55555555555, @ smth, 55555555555, water is dead
2005-4-16
Zaiben @ smth, 55555555555, @ smth, 55555555555, water is dead 2005-4-16