实验六 MVC及Struts2的使用

完整实验报告


实验六  MVCStruts2的使用

一、实验目的               

1.了解MVC思想。

2.熟悉Struts2框架下的开发方法。

二、实验学时

2H

三、实验性质

综合性实验

四、实验内容

1.创建表t_student,其中包含学号stuno,姓名stuname,性别stusex三个字段,在表中插入一些测试数据。(5分)

2.使用Struts2框架来实现下述功能:

  1. 编写显示所有学生资料信息页面,其中包含一个表单用来根据学生姓名模糊查询匹配的学生信息。(50分)
  2. 在学生信息后面增加一个“删除学生信息”链接,单击链接可以将学生信息从数据库中删除,然后跳转到显示所有学生资料信息页面。(25分)

index.jsp里的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>首页面</title>

</head>

<body>

首页面

<a href="find.action">查询所有学生数据</a>

</body>

</html>

student_find.jsp里的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>查询所有学生数据</title>

</head>

<body>

<form action="query.action" method="get">

请输入要查询的学生姓名(支持模糊查询) <input type="text" name="stuname"

placeholder="学生姓名"> <input type="submit" value="查询">

</form>

<table>

<tr>

<td>学号</td>

<td>姓名</td>

<td>性别</td>

<td>操作</td>

</tr>

<c:forEach items="${students }" var="stu">

<tr>

<td>${stu.stuno }</td>

<td>${stu.stuname }</td>

<td>${stu.stusex }</td>

<td><a href="delete.action?stuno=${stu.stuno }">删除</a> <a

href="update.action?stuno=${stu.stuno }">修改</a></td>

</tr>

</c:forEach>

</table>

<a href="student_add.jsp">添加学生数据</a>

</body>

</html>

用<form action="query.action" method="get">请输入要查询的学生姓名(支持模糊查询) <input type="text" name="stuname"placeholder="学生姓名"> <input type="submit" value="查询"></form>写出这个简单的表单。

Student.java里的代码:

package com.lab.model;

public class Student {

private String stuno;

private String stuname;

private String stusex;

public Student() {

}

public Student(String stuno, String stuname, String stusex) {

super();

this.stuno = stuno;

this.stuname = stuname;

this.stusex = stusex;

}

public String getStuno() {

return stuno;

}

public void setStuno(String stuno) {

this.stuno = stuno;

}

public String getStuname() {

return stuname;

}

public void setStuname(String stuname) {

this.stuname = stuname;

}

public String getStusex() {

return stusex;

}

public void setStusex(String stusex) {

this.stusex = stusex;

}

@Override

public String toString() {

return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stusex=" + stusex + "]";

}

}

把stuno,stuname,stusex用getters和setters方法进行封装,再生成构造方法。

StudentDao.java里的代码:

package com.lab.dao;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

import com.lab.model.Student;

import com.lab.util.DatabaseBean;

public class StudentDao  {

Connection conn = null;

PreparedStatement psmt = null;

ResultSet rs = null;

public List<Student> queryAllStudents() {

List<Student> students = new ArrayList<Student>();

try {

conn = DatabaseBean.getConnection();

psmt = conn.prepareStatement("select * from t_student ");

ResultSet rs = psmt.executeQuery();

while(rs.next()) {

Student stu = new Student();

stu.setStuno(rs.getString("stuno"));

stu.setStuname(rs.getString("stuname"));

stu.setStusex(rs.getString("stusex"));

students.add(stu);

}

}catch (SQLException e) {

e.printStackTrace();

} finally {

DatabaseBean.close(rs, psmt, conn);

}

return students;

}

public List<Student> queryAllStudents(String stuname) {

List<Student> students = new ArrayList<Student>();

try {

conn = DatabaseBean.getConnection();

psmt = conn.prepareStatement("select * from t_student where stuname like ?");

psmt.setString(1, "%"+stuname+"%");

ResultSet rs = psmt.executeQuery();

while(rs.next()) {

Student stu = new Student();

stu.setStuno(rs.getString("stuno"));

stu.setStuname(rs.getString("stuname"));

stu.setStusex(rs.getString("stusex"));

students.add(stu);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

DatabaseBean.close(rs, psmt, conn);

}

return students;

}

public boolean deleteStudent(String stuno) {

try {

conn = DatabaseBean.getConnection();

psmt = conn.prepareStatement("delete from t_student where stuno=?");

psmt.setString(1, stuno);

int result = psmt.executeUpdate();

psmt.close();

conn.close();

if(result > 0) {

return true;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

DatabaseBean.close(rs, psmt, conn);

}

return false;

}

public boolean addStudent(Student stu) {

try {

conn = DatabaseBean.getConnection();

PreparedStatement psmt = conn.prepareStatement("insert into t_student values(?,?,?)");

psmt.setString(1, stu.getStuno());

psmt.setString(2, stu.getStuname());

psmt.setString(3, stu.getStusex());

int result = psmt.executeUpdate();

psmt.close();

conn.close();

if(result > 0) {

return true;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

DatabaseBean.close(rs, psmt, conn);

}

return false;

}

public boolean updateStudent(Student stu) {

try {

conn = DatabaseBean.getConnection();

PreparedStatement psmt = conn.prepareStatement("update t_student set stuname=?,stusex=? where stuno=?");

psmt.setString(1, stu.getStuname());

psmt.setString(2, stu.getStusex());

psmt.setString(3, stu.getStuno());

int result = psmt.executeUpdate();

psmt.close();

conn.close();

if(result > 0) {

return true;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

DatabaseBean.close(rs, psmt, conn);

}

return false;

}

public Student queryStudent(String stuno) {

Student stu = null;

try {

conn = DatabaseBean.getConnection();

PreparedStatement psmt = conn.prepareStatement("select * from t_student where stuno=?");

psmt.setString(1, stuno);

ResultSet rs = psmt.executeQuery();

if(rs.next()) {

stu = new Student();

stu.setStuno(rs.getString("stuno"));

stu.setStuname(rs.getString("stuname"));

stu.setStusex(rs.getString("stusex"));

}

rs.close();

psmt.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

} finally {

DatabaseBean.close(rs, psmt, conn);

}

return stu;

}

}

定义了三个变量分别是数据库连接的变量,预编译对象变量,结果集变量,写了查询所有学生数据的方法,根据学生姓名查询学生数据的方法,删除学生数据的方法,添加学生数据的方法最后是根据学生学号学生数据的方法

DatabaseBean.java里的代码:

package com.lab.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class DatabaseBean {

public static Connection getConnection() {

Connection conn = null;

try {

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

String path = " C:/Users/李心洁/Documents/Database6.accdb";

String url = "jdbc:ucanaccess://" + path;

conn = DriverManager.getConnection(url);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static void close(ResultSet rs, PreparedStatement psmt, Connection conn) {

try {

if(rs!=null) {

rs.close();

}

if(psmt!=null) {

psmt.close();

}

if(conn!=null) {

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

定义了两个静态方法,第一个方法就是用来获取数据库连接,第二个方法用来关闭数据库资源的静态方法。

StudentAction.java里的代码:

package com.lab.action;

import java.util.List;

import com.lab.dao.StudentDao;

import com.lab.model.Student;

public class StudentAction extends SuperAciton {

StudentDao studentDao =new StudentDao();

public String find() {

List<Student> students = studentDao.queryAllStudents();

request.setAttribute("students", students);

return "find_success";

}

public String query() {

String stuname = request.getParameter("stuname");

List<Student> students = studentDao.queryAllStudents(stuname);

request.setAttribute("students", students);

return "query_success";

}

public String delete() {

String stuno = request.getParameter("stuno");

studentDao.deleteStudent(stuno);

return "delete_success";

}

public String add() {

String stuno = request.getParameter("stuno");

String stuname = request.getParameter("stuname");

String stusex = request.getParameter("stusex");

Student stu = new Student(stuno,stuname,stusex);

boolean flag = studentDao.addStudent(stu);

if(flag) {

return "add_success";

}else {

return "add_failure";

}

}

public String update() {

String stuno = request.getParameter("stuno");

Student student=studentDao.queryStudent(stuno);

request.setAttribute("student", student);

return "update_success";

}

public String save() {

String stuno = request.getParameter("stuno");

String stuname = request.getParameter("stuname");

String stusex = request.getParameter("stusex");

Student stu = new Student(stuno,stuname,stusex);

boolean flag = studentDao.updateStudent(stu);

if(flag) {

return "save_success";

}else {

return "save_failure";

}

}

}

查询所有学生的动作、根据姓名模糊查询匹配学生的动作,删除所有学生的动作、添加所有学生的动作、修改所有学生的动作、保存所有学生的动作。

SuperAction.java里的代码:

package com.lab.action;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import org.apache.struts2.interceptor.ServletResponseAware;

import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class SuperAciton extends ActionSupport implements ServletRequestAware, ServletResponseAware,ServletContextAware {

private static final long serialVersionUID = 1L;

protected HttpServletRequest request;

protected HttpServletResponse response;

protected HttpSession session;

protected ServletContext application;

@Override

public void setServletContext(ServletContext application) {

this.application = application;

}

@Override

public void setServletResponse(HttpServletResponse response) {

this.response = response;

}

@Override

public void setServletRequest(HttpServletRequest request) {

this.request = request;

this.session = request.getSession();

}

}

实现了ServletRequestAware、ServletResponseAware、ServletContextAware这三个接口,定义了request、response、application这三个对象实现这三个方法,定义这几个内置对象可以在我们程序里直接使用

struts.xml里的代码:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

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

<default-action-ref name="index" />

<action name="index">

<result>index.jsp</result>

</action>

<action name="*" class="com.lab.action.StudentAction"

method="{1}">

<result name="find_success">student_find.jsp</result>

<result name="query_success">student_find.jsp</result>

<result name="delete_success" type="redirectAction">find</result>

<result name="add_success" type="redirectAction">find</result>

<result name="add_failure" type="redirect">student_add.jsp</result>

<result name="update_success">student_update.jsp</result>

<result name="save_success" type="redirectAction">find</result>

<result name="save_failure" type="chain">update</result>

</action>

</package>

<include file="example.xml" />

<!-- Add packages here -->

</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>lab6</display-name>

  

  <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>

程序参考界面如图1和图2所示:

 

3.提高题: 尝试实现学生信息的添加和修改功能。(10分)

student_add.jsp里的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>添加学生信息</title>

<style>

.title {

text-align: center;

}

.error {

padding-left: 100px;

color: red;

font-weight: bold;

}

#loginDiv {

width: 400px;

border: 1px solid #666;

margin: 20px auto;

padding: 50px;

}

.input {

padding: 10px;

}

.input span {

display: inline-block;

width: 100px;

}

.input_code {

width: 100px;

}

.button {

margin-top:10px;

text-align: center;

}

input {

width: 250px;

padding: 5px;

}

input[type='submit'] {

width: 100px;

padding: 5px;

}

input[type='radio'] {

width: 20px;

padding: 5px;

}

</style>

</head>

<body>

<h2 class="title">添加学生信息</h2>

<div id="loginDiv">

<form method="post" name="addForm" action="add.action">

<div class="input">

<span>学号:</span><input name="stuno" type="text" placeholder="请输入学生学号">

</div>

<div class="input">

<span>姓名:</span><input name="stuname" type="text" placeholder="请输入学生姓名">

</div>

<div class="input">

<span>性别:</span>

<input type="radio" name="stusex" value="男" checked>男  

<input type="radio" name="stusex" value="女">女

</div>

<div class="button">

<input type="submit" value="添加">

</div>

</form>

</div>

<div align="center"><a href="find">显示学生信息</a></div>

</body>

</html>

student_update.jsp里的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>修改学生信息</title>

<style>

.title {

text-align: center;

}

.error {

padding-left: 100px;

color: red;

font-weight: bold;

}

#loginDiv {

width: 400px;

border: 1px solid #666;

margin: 20px auto;

padding: 50px;

}

.input {

padding: 10px;

}

.input span {

display: inline-block;

width: 100px;

}

.input_code {

width: 100px;

}

.button {

margin-top: 10px;

text-align: center;

}

input {

width: 250px;

padding: 5px;

}

input[type='submit'] {

width: 100px;

padding: 5px;

}

input[type='radio'] {

width: 20px;

padding: 5px;

}

</style>

</head>

<body>

<h2 class="title">修改学生信息</h2>

<div id="loginDiv">

<form method="post" name="updateForm" action="save.action">

<div class="input">

<span>学号:</span><input name="stuno" type="text"

placeholder="请输入学生学号" value="${student.stuno }" readonly="readonly">

</div>

<div class="input">

<span>姓名:</span><input name="stuname" type="text"

placeholder="请输入学生姓名" value="${student.stuname }">

</div>

<div class="input">

<span>性别:</span>

<c:if test="${student.stusex=='男' }">

<input type="radio" name="stusex" value="男" checked>男  

<input type="radio" name="stusex" value="女">女

</c:if>

<c:if test="${student.stusex=='女' }">

<input type="radio" name="stusex" value="男">男  

<input type="radio" name="stusex" value="女" checked>女

</c:if>

</div>

<div class="button">

<input type="submit" value="保存">

</div>

</form>

</div>

<div align="center">

<a href="find">显示学生信息</a>

</div>

</body>

</html>

 

 

 

 

 

 五、实验总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值