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>

MVC基于Struts2的CRUD,java+bean+struts的更多相关文章

  1. 【整理】JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系

    #[整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 ![关系图解](http://images.cnitblog.com/blog/84 ...

  2. 基于Struts2 的日志管理系统的Java实现

    1.首先,项目的架构如下:          2.com.sxl.dba 中:OracleConnector.java package com.sxl.dba; import java.sql.*; ...

  3. JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系

    郭晨 软件151 1531610114 [整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 visio文件下载 概述 一个JavaEE的项 ...

  4. Java bean与Spring、Spring MVC关系

    Java Bean Java语言欠缺属性.事件.多重继承功能.所以,如果要在Java程序中实现一些面向对象编程的常见需求,只能手写大量胶水代码.Java Bean正是编写这套胶水代码的惯用模式或约定. ...

  5. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  6. Spring、Spring MVC、Struts2优缺点整理

    Spring 及其优点 大部分项目都少不了Spring的身影,为什么大家对他如此青睐,而且对他的追捧丝毫没有减退之势呢 Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量 ...

  7. Spring、Spring MVC、Struts2、、优缺点整理(转)

    Spring 及其优点 大部分项目都少不了spring的身影,为什么大家对他如此青睐,而且对他的追捧丝毫没有减退之势呢 Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量 ...

  8. Spring MVC和Struts2的比较的优点

    Spring MVC和Struts2的区别: 机制:spring mvc的入口是servlet,而struts2是filter(这里要指出,filter和servlet是不同的.以前认为filter是 ...

  9. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

随机推荐

  1. JavaThread等待/通知经典范式

    JavaThread等待/通知经典范式 package com.stono.thread; import java.text.SimpleDateFormat; import java.util.Da ...

  2. 允许远程访问MySQL的设置

    允许远程访问MySQL的设置 学习了:http://www.cnblogs.com/hyzhou/archive/2011/12/06/2278236.html Windows版本有workbench ...

  3. 自己写的php curl库实现整站克隆

    有时候常常会用到一些在线手冊,比方国内或国外的.有些是訪问速度慢,有些是作者直接吧站点关闭了,有些是server总是宕机.所以还是全盘克隆到自己server比較爽.所 已这里给了一个demo < ...

  4. Android Internet - WebView 的使用

    WebView是Android 提供的操作网页的一个组件. 用于浏览网页及其它Internet资源. 这里总结了一些WebView 的经常使用接口.和2个小演示样例程序用于自己开发时直接使用.就不用再 ...

  5. PyCharm 运行工程

  6. Android 怎样实现 焦点图的 无线循环滑动的状态?

    參考网址:http://my.oschina.net/xsk/blog/119167 总体的架构:ViewPgaer 中直接嵌套  IamgeView 方案一:  重写Viewpager 这样有局限性 ...

  7. 《Java程序设计》第16周周五:数据库连接 与 随机数的使用

    第一部分:实验项目 项目二:数据库初步. 目的:了解Java连接数据库的步骤与方法.以及MySQL数据库的安装与使用. 目标: (1)在机房安装上MySQL数据库. 安装成功 MySQL数据库 (2) ...

  8. 软件开发 —— 重构(refactor)

    0. 代码坏味道 Large Class,过大的类:Large method,过长的(成员)函数: 1. 基本内涵 在不改变代码外在行为的前提下对代码做出修改,以改进代码的内部结构的过程. -- &l ...

  9. H3C交换机DHCP&nbsp;Server配置的六个方面

    H3C交换机DHCP Server配置的六个方面 在交换机上面配置DHCP内容是司空见惯的了.那么这里我们就讲解一下H3C交换机DHCP Server配置内容.之后的文章中,我们还对针对其他方面的配置 ...

  10. BZOJ 4491 分块OR差分+线段树

    思路: (是不是只有我作大死写了个分块) up[i][j]表示从第i块开始到第j个位置 上升的最大值 down[i][j]同理 left_up[i]表示从第i块开始能够上升的最长长度 left_dow ...