普通 Javaweb項目模板的搭建
1、 創建一個web項目模板的maven項目
2、配置 Tomcat 服務器
3、先測試一下該空項目
4、注入 maven 依賴
5、創建項目的包結構
6、編寫基礎公共類
6.1、 數據庫配置文件(db.properties)
6.2、 編寫數據庫的公共類
1、 創建一個web項目模板的maven項目


現在就是一個web項目了。
2、 配置 Tomcat 服務器




現在就配置好 Tomcat 服務器了!
3、先測試一下該空項目


如果能夠正常訪問到頁面,則服務器配置沒有什么問題;否則,則出現問題。那就得另行解決了。。。。
4、 注入 maven 依賴
web項目需要的jar包:jsp,servlet,mysql驅動,jdbc,jstl,standard .....
<!-- servlet依賴 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP依賴 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!--JSTL表達式依賴-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--standard標簽庫-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--jdbc -->
<dependency>
<groupId>org.clojure</groupId>
<artifactId>java.jdbc</artifactId>
<version>0.7.11</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--連接mysql數據庫依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--fastjson依賴-處理json字符串-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
5、 創建項目的包結構

提前建好包,結構清晰,邏輯分明,方便代碼的編寫。。。。。
6、 編寫基礎公共類
6.1、 數據庫配置文件(db.properties)
#加載驅動
driver=com.mysql.jdbc.Driver
#加載數據庫
#dbname是要連接的數據庫名稱
url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf-8
#用戶名
user=root
#密碼
password=123456
6.2、 編寫數據庫的公共類
public class DBUtil {
//靜態代碼塊,在類加載的時候執行
static{
init();
}
private static String driver;
private static String url;
private static String user;
private static String password;
//初始化連接參數,從配置文件里獲得
public static void init(){
Properties params=new Properties();
String configFile = "db.properties";
InputStream is= DBUtil.class.getClassLoader().getResourceAsStream(configFile);
try {
params.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driver=params.getProperty("driver");
url=params.getProperty("url");
user=params.getProperty("user");
password=params.getProperty("password");
}
/*
* 獲得數據庫連接
* */
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/*
* 查詢操作
* params 是prepareStatement所執行sql語句中 占位符 ? 的值
* */
public static ResultSet execute(Connection connection, PreparedStatement pstm, ResultSet rs,
String sql, Object[] params) throws Exception{
pstm = connection.prepareStatement(sql);
for(int i = 0; i < params.length; i++){
pstm.setObject(i+1, params[i]);
}
rs = pstm.executeQuery();
return rs;
}
/*
* 更新操作
* */
public static int execute(Connection connection,PreparedStatement pstm,
String sql,Object[] params) throws Exception{
int updateRows = 0;
pstm = connection.prepareStatement(sql);
for(int i = 0; i < params.length; i++){
pstm.setObject(i+1, params[i]);
}
updateRows = pstm.executeUpdate();
return updateRows;
}
/*
* 資源回收
* */
public static boolean closeResource(Connection connection,PreparedStatement pstm,ResultSet rs){
boolean flag = true;
if(rs != null){
try {
rs.close();
rs = null;//GC回收
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
}
if(pstm != null){
try {
pstm.close();
pstm = null;//GC回收
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
}
if(connection != null){
try {
connection.close();
connection = null;//GC回收
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
}
return flag;
}
至此web項目的模板就已經構建的差不多了.....
不知道怎么回事,通過先創建一個普通 maven 項目,然后再將其變為一個 web 項目的方式,一直報 找不到過濾器 這個錯誤;必須通過使用模板構建的方式才能夠使用過濾器!