eclipse+struts2.5.22的配置和案例

注意:不一樣的struts2版本,web.xml的配置可能不一樣html

1.進入MyEclipse環境,新建一個Web Project。java

2. 設計數據庫表結構。mysql

CREATE TABLE `book` (web

  `ID` varchar(255) NOT NULL,sql

  `Name` varchar(255) DEFAULT NULL,數據庫

  `Type` varchar(255) DEFAULT NULL,apache

  `Author` varchar(255) DEFAULT NULL,app

  PRIMARY KEY (`ID`)jsp

)post

3. 採用Struts2技術完成數據庫的CRUD。

Struts2版本:2.5.22

Struts2下載網址:https://struts.apache.org/download.cgi#struts2522

下載struts-2.5.22-all.zip,解壓

解壓後目錄:

到lib文件夾中找到須要的jar包放到項目的/WEB-INF/lib下

總目錄結構:

struts2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="default" namespace="/action" extends="struts-default">

    <action name="BookAdd" class="action.BookAdd">
        <result name="Success">/index.jsp</result>
        <result name="Error">/Error.jsp</result>
    </action>
    <action name="BookEdit" class="action.BookEdit">
        <result name="Success">/index.jsp</result>
        <result name="Error">/Error.jsp</result>
    </action>
        <action name="BookDelete" class="action.BookDelete">
        <result name="Success">/index.jsp</result>
        <result name="Error">/Error.jsp</result>
    </action>
</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>實驗二</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
       <filter>
    <filter-name>Struts2</filter-name>
    <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form name="f1" id="f1" action="action/BookAdd.action" method="post">
    <table align="center">
        <h3 align="center">圖書管理系統</h1>
        <tr>
            <td><a href="bookadd.jsp">圖書添加</a></td>
        </tr>
        <tr>
            <td><a href="booklist.jsp">圖書列表</a></td>
        </tr>
    </table>
</form>
</body>
</html>

bookadd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bookadd.jsp</title>
</head>
<body>
<form name="f1" id="f1" action="action/BookAdd.action" method="post">
    <h3 align="center">圖書添加</h3>
    
    <table align="center">
        <tr>
            <td colspan="2" align="center"><a href="index.jsp">返回</a></td>
        </tr>
    
        <tr>
            <td>編號:</td>
            <td><input id="ID" name="ID"></td>
        </tr>
        <tr>
            <td>書名:</td>
            <td><input id="Name" name="Name"></td>
        </tr>
        <tr>
            <td>類型:</td>
            <td><input id="Type" name="Type"></td>
        </tr>
        <tr>
            <td>做者:</td>
            <td><input id="Author" name="Author"></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value=" 肯定 " ></td>
        </tr>
    </table>
</form>
</body>
</html>

Error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <%
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>My JSP 'Error.jsp' starting page</title>
</head>
<body>
<form>
    <h3 align="center"><s:property value="msg" /> <br></h3>
    
    <table align="center">
        <tr>
            <td colspan="2" align="center"><a href="index.jsp">返回</a></td>
        </tr>
    </table>
</form>
</body>
</html>

 

BookAdd.java

package action; import java.sql.Connection; import java.sql.Statement; import com.opensymphony.xwork2.ActionSupport; import util.DBUtil; public class BookAdd extends ActionSupport{ private String ID; private String Name; private String Type; private String Author; private String Msg; public String execute() throws Exception { Connection conn = DBUtil.getConn(); String sql = "insert into book(ID,Name,Type,Author) values('" + getID() + "','" +getName()+"','"+getType()+"','"+getAuthor()+"')"; Statement state = null; try { state = conn.createStatement(); state.executeUpdate(sql); }catch (Exception e) { setMsg("編號重複!"); return "Error"; } return "Success"; } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getType() { return Type; } public void setType(String type) { Type = type; } public String getAuthor() { return Author; } public void setAuthor(String author) { Author = author; } public String getMsg() { return Msg; } public void setMsg(String msg) { Msg = msg; } }

DBUtil.java

package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/lianxi?useUnicode=true&characterEncoding=UTF-8"; public static String db_user = "root"; public static String db_password = "liu123"; public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_password); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void close(Statement state, Connection conn) { if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
相關文章
相關標籤/搜索