regist.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Regist</title>
</head>
<body>
<form action="regist.action" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type = "text" name = "name"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type = "text" name = "password"></td>
</tr>
<tr>
<td><input type = "submit" value="提交"></td>
<td><input type = "reset" value="清空"></td>
</tr>
</table>
</form>
</body>
</html>
success.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success.....
</body>
</html>
error.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
error.......
</body>
</html>
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<!--<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="tags" extends="struts-default">
<action name="regist" class="test.RegistAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
dbcp.properties文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=jftt
initialSize=2
maxActive=10
maxIdle=2
minIdle=1
maxWait=30000
DbUtil.java文件
package test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DbUtil {
private static DataSource ds = null;
static{
try{
Properties props = new Properties();
props.load(DbUtil.class.getClassLoader().getResourceAsStream("dbcp.properties"));
ds = BasicDataSourceFactory.createDataSource(props);
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void closeConnection(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
RegistAction.java文件
package test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class RegistAction {
private String name;
private String password;
public String execute(){
String sql = "insert into d_user " +
" (name,password)" +
" values (?,?)";
Connection conn = DbUtil.getConnection();
PreparedStatement ps;
if(name == null||name.length()==0||password == null||password.length()==0){
return "error";
}
try {
ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, name);
ps.setString(2, password);
ps.executeUpdate();
return "success";
} catch (SQLException e) {
e.printStackTrace();
return "error";
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
新建一个包叫test
里面放置
DbUtil.java
RegistAction.java
文件dbcp.properties和文件struts.xml
放在test包外面,跟test包平级;
web.xml文件放在WEB-INF文件夹里面
error.jsp
regist.jsp
success.jsp
这三个界面放在WEB-INF文件夹外面,跟WEB-INF文件夹平级
需要导包:
大概如下,版本不同没有关系
commons-collections-3.1.jar
commons-dbcp-1.2.2.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
commons-pool.jar
freemarker-2.3.15.jar
log4j-1.2.11.jar
mysqldriver.jar
ognl-2.7.3.jar
servlet-api.jar
struts2-core-2.1.8.jar
struts2-json-plugin-2.1.8.jar
xwork-core-2.1.6.jar