zoukankan      html  css  js  c++  java
  • MVC基于Struts2的CRUD,java+bean+struts

    1,所需jar包,将jar包导入lib中

    2,项目目录结构

    3,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.3.dtd">
        
    <struts>
    
    <package name="default" namespace="/" extends="struts-default">
            <action name="DelStu" class="struts2.DelStu">
                <result>/index.jsp</result>
            </action>
            
            <action name="UpdateStu" class="struts2.UpdateStu">
                <result>/index.jsp</result>
            </action>
            
            <action name="AddStu" class="struts2.AddStu">
                <result>/index.jsp</result>
            </action>
        </package>
        
    </struts>

    4,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>JE-sy2</display-name>
      <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    <!--   Struts2的框架的过滤器 -->
          <filter>
            <filter-name>struts2</filter-name>
            <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>

    5,StudentDaoImpl.java

    package dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import bean.Student;
    import util.DBUtil;
    
    public class StudentDaoImpl {
        public void addStudent(Student student)
        {
           Connection connection = DBUtil.getConnection();
           String sql = "insert into student (stuId,stuName) values (?,?)";
           PreparedStatement preparedStatement = null;
           try {
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1,student.getStuId());
            preparedStatement.setString(2, student.getStuName());
        
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }    
        }
    
        public void deleteStudent(String stuId) {
               Connection connection = DBUtil.getConnection();
               String sql = "delete from student where stuId= ?";
               PreparedStatement preparedStatement = null;
               try {
                preparedStatement= connection.prepareStatement(sql);
                preparedStatement.setString(1, stuId);
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
    
        public void updateStudent(Student student) {
               Connection connection = DBUtil.getConnection();
               String sql = "update student set stuName = ? where stuId=?";
               PreparedStatement preparedStatement = null;
               try {
                preparedStatement= connection.prepareStatement(sql);
                preparedStatement.setString(1, student.getStuName());
                preparedStatement.setString(2, student.getStuId());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }    
            
        }
    
        public List<Student> loadStudent() {
               Connection connection = DBUtil.getConnection();
               String sql = "select * from student";
               PreparedStatement preparedStatement = null;
               ResultSet resultSet=null;
               Student student = null;
               List<Student> students=new ArrayList<>();
               try {
                preparedStatement=connection.prepareStatement(sql);
    //            preparedStatement.setInt(1, id);
                resultSet=preparedStatement.executeQuery();
                while(resultSet.next())
                {
                    student=new Student();
                    student.setStuId(resultSet.getString("stuId"));
                    student.setStuName(resultSet.getString("stuName"));
                    students.add(student);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            return students;
            
        }
    }

    6,AddStu.java

    package struts2;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    import bean.Student;
    import dao.StudentDaoImpl;
    
    public class AddStu extends ActionSupport{
        HttpServletRequest request = ServletActionContext.getRequest();
        private static final long serialVersionUID = 1L;
        @Override
        public String execute() throws Exception {
        
            request.setCharacterEncoding("UTF-8");
            String stuId=request.getParameter("stuId");
            String stuName=request.getParameter("stuName");
            request.setAttribute("name",stuName);//设值
            
            Student student=new Student();
            student.setStuId(stuId);
            student.setStuName(stuName);
            StudentDaoImpl studentDaoImpl=new StudentDaoImpl();
            studentDaoImpl.addStudent(student);
    //        response.sendRedirect("CRUD/index.jsp");
            return SUCCESS;
        }
    }

    7,addStu.jsp

    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.List"%>
    <%@page import="dao.StudentDaoImpl"%>
    <%@page import="bean.Student"%>
    <%@ 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>Insert title here</title>
    </head>
    <%
        Student student=new Student();
        StudentDaoImpl studentDaoImpl=new StudentDaoImpl();
        List<Student> students=studentDaoImpl.loadStudent();
    //     System.out.println(request.getContextPath());
    %>
    <body>
        <center>
            <form action="AddStu.action" method="post">
                <table>
                    <tr>
                        <td>学号:</td>
                        <td><input type="text" name="stuId"/></td>
                    </tr>
                    <tr>
                        <td>姓名:</td>
                        <td><input type="text" name="stuName"/></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="增加"/></td>
                    </tr>
                </table>
            </form>
        </center>
    </body>
    </html>
  • 相关阅读:
    Shell重新学习(忘光了)
    vim 设置默认显示行号
    maven学习资料(三)
    maven:新建的maven工程需要添加一下插件
    Spring框架:第五章:Spring EL表达式
    Spring框架:第四章:Spring管理数据库连接池
    Spring框架:第三章:对象的生命周期及单例bean生命周期的11个步骤
    Spring框架:第二章:IOC依赖注入及40个实验
    Spring框架:第一章:介绍和准备工作
    MyBatis框架:第十一章:mybatis 逆向工程
  • 原文地址:https://www.cnblogs.com/news1997/p/10906490.html
Copyright © 2011-2022 走看看