I believe that many cainiao who are new to struts will admire the broad and profound Struts framework. I am also a member of the large team, the first to use the struts built-in struts-config.xml data sources to connect to MySQL, there is always a problem: Error 404 action is not available ...... this is a problem. as I was just getting started with Struts, my head immediately became blank and I felt like I had nowhere to start. So I started to search for Google and Baidu on the Internet, however, there are many items to copy from copy, which may be a common problem on the Internet. at the beginning, I started to use the code for the test. However, no matter what the problem is, the problem still persists. This makes me more confused on the basis of a blank space. I suspect there is a problem with my browser, I suspect there is a problem with my installed eclipse environment, and I suspect there is a problem with the search examples ......
On the Internet, I often see other people saying that if you want to improve your technology, you need to read more English documents. Because your e-article is still limited, you should first quit when you encounter something in e-article, therefore, I also missed a lot. in the case of helplessness, I am determined to eat e-article and start my own lame e-Article journey.
"Eat" e-Wen has been around for a while. Now, although it seems that the level of e-Wen has not improved much, I feel that it is a good document during reading e-Wen, A good website provides clear explanations and concise syntaxes. Apart from the translation of some words, you can understand the meaning at a Glance. Oh, my God! Great :)
My solution to the data source is to get help from the e-file website. I don't want to talk about it anymore. I hope my brothers and sisters will have time to read the original documents (mostly e-files), won't they ?? If you have a hard head, you must watch it too ......:)
Configure the struts data source on Tomcat 5:
1: download and install the tomcat5.5.9 http://jakarta.apache.org/tomcat/http://struts.apache.org/download.cgi
3: Download MySQL JDBC driver mysql-connector-java-3.0.16-ga-bin.jar (Google)
4: Create a MySQL database name: strutsdatabase
5. Create a MySQL database table:
Create Table 'test '(
'Username' varchar (20) not null default''
) Type = MyISAM;
/* Data for the table 'test '*/
Insert into 'test' values ('rajesh'), ('George '), ('vikas'), ('prakash'), ('mahesh ');
6: the elements in the data-Source of the struts-config.xml should be like this:
<data-sources>
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property
property="url"
value="jdbc:mysql://localhost:3306/strutsdatabase?autoReconnect=true" />
<set-property
property="username"
value="root" />
<set-property
property="password"
value="" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM test" />
</data-source>
</data-sources>
7. Create an action class to test the datasource.
package test;
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.*;
public class TestDataSource extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
javax.sql.DataSource dataSource;
java.sql.Connection myConnection=null;
try {
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
Statement stmt=myConnection.createStatement();
ResultSet rst=stmt.executeQuery("select username from test");
System.out.println("******************************************");
System.out.println("********Out Put from TestDataSource ******");
while(rst.next()){
System.out.println("User Name is: " + rst.getString("username"));
}
System.out.println("******************************************");
rst.close();
stmt.close();
// do what you wish with myConnection
} catch (SQLException sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//enclose this in a finally block to make
//sure the connection is closed
try {
myConnection.close();
} catch (SQLException e) {
getServlet().log("Connection.close", e);
}
}
return mapping.findForward("success");
}
}
8: In
Add the action mapping element to the struts-config.xml.
<Action
Path = "/datasource"
Type = "test. testdatasource">
<Forward name = "success" Path = "/success. jsp"/>
</Action>
9: Create a success. jsp file in the root directory of your strutsdatabase
(Just write something like hello World! :))
10: test your results: http: // localhost: 8080/strutsdatabase/datasource. Do
11: Check whether your browser displays: Hello world! Haha!
2: Download struts1.2.7