zoukankan      html  css  js  c++  java
  • eclipse+struts2.5.22的配置和案例

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

    1.进入MyEclipse环境,新建一个Web Project。

    2. 设计数据库表结构。

    CREATE TABLE `book` (

      `ID` varchar(255) NOT NULL,

      `Name` varchar(255) DEFAULT NULL,

      `Type` varchar(255) DEFAULT NULL,

      `Author` varchar(255) DEFAULT NULL,

      PRIMARY KEY (`ID`)

    )

    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();
                }
            }
        }
    
    }
  • 相关阅读:
    “无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法
    闹洞房小游戏
    第3章 Oracle实例的管理
    公积金余额查询电话
    人要有忧患意识
    第0章 Oracle的安装及相关配置
    第5章 重做日志文件
    oracle体系结构视频学习笔记
    编写存储过程实现查询mysql数据库中的表名并删除表中的数据
    第6章 表空间和数据文件的管理
  • 原文地址:https://www.cnblogs.com/liujinxin123/p/12623014.html
Copyright © 2011-2022 走看看