struts综合案例

用到的主要知识点:模型驱动、数据回显、防止表单重复提交、请求数据自动封装等

一、数据库准备

新建student数据表,id(int),name,age(int),sex四个字段

二、项目环境搭建

1.添加struts支持,引入jar包

c3p0-0.9.1.2.jar
commons-dbutils-1.6.jar
MySQL-connector-Java-5.1.12-bin.jar

2.复制c3p0.xml文件

<c3p0-config>
	<default-config>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/数据库名
		</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="user">root</property>
		<property name="password">数据库密码</property>
		<property name="initialPoolSize">3</property>
		<property name="maxPoolSize">6</property>
		<property name="maxIdleTime">1000</property>
	</default-config>
</c3p0-config>

3.复制jdbcUtil文件

package utils;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
 * 工具类
 * 1. 初始化C3P0连接池
 * 2. 创建DbUtils核心工具类对象
 * @author Jie.Yuan
 *
 */
public class JdbcUtils {
	/**
	 *  1. 初始化C3P0连接池
	 */
	private static  DataSource dataSource;
	static {
		dataSource = new ComboPooledDataSource();
	}	
	/**
	 * 2. 创建DbUtils核心工具类对象
	 */
	public static QueryRunner getQueryRuner(){
		// 创建QueryRunner对象,传入连接池对象
		// 在创建QueryRunner对象的时候,如果传入了数据源对象;
		// 那么在使用QueryRunner对象方法的时候,就不需要传入连接对象;
		// 会自动从数据源中获取连接(不用关闭连接)
		return new QueryRunner(dataSource);
	}
}

三、项目开发

1.新建实体类student

package cn.itcast.entity;

public class Student {
	private int id;
	private String name;
	private int age;
	private String sex;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age
				+ ", sex=" + sex + "]";
	}

}

2.写dao层文件

数据访问层接口:

package cn.itcast.dao;

import java.util.List;

import cn.itcast.entity.Student;

public interface IEmployeeDao {
	/*
	 * 查询全部学生信息
	 */
	public List<Student> findAll();
	
	/*
	 * 查询单个学生信息
	 */
	public Student findOne(int id);
	
	/*
	 * 修改单个学生信息
	 */
	public void update(Student student);
	
	/*
	 * 删除单个学生信息
	 */
	public void delete(int id);
	
	/*
	 * 增加单个学生信息
	 */
	public void add(Student student);
}
数据访问层实现类:
package cn.itcast.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.enterprise.inject.New;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import utils.JdbcUtils;
import cn.itcast.dao.IEmployeeDao;
import cn.itcast.entity.Student;

public class EmployeeDao implements IEmployeeDao{

	@Override
	public List<Student> findAll() {
		String sql="select * from student";
		try {
			return JdbcUtils.getQueryRuner().query(sql, new BeanListHandler<Student>(Student.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
	}

	@Override
	public Student findOne(int id) {
		String sql="select * from student where id=?";
		try {
			return JdbcUtils.getQueryRuner().query(sql, new BeanHandler<Student>(Student.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
	}

	@Override
	public void update(Student student) {
		// TODO Auto-generated method stub
		String sql="update student set name=?,age=?,sex=? where id=?";
		try {
			JdbcUtils.getQueryRuner().update(sql,student.getName(),student.getAge(),student.getSex(),student.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		String sql="delete from student where id=?";
		try {
			JdbcUtils.getQueryRuner().update(sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void add(Student student) {
		// TODO Auto-generated method stub
		String sql="insert into student(name,age,sex) values(?,?,?)";
		try {
			JdbcUtils.getQueryRuner().update(sql, student.getName(),student.getAge(),student.getSex());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

3.service文件

服务层实现类:

package cn.itcast.service.impl;

import java.sql.SQLException;
import java.util.List;

import javax.enterprise.inject.New;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import utils.JdbcUtils;
import cn.itcast.dao.IEmployeeDao;
import cn.itcast.dao.impl.EmployeeDao;
import cn.itcast.entity.Student;

public class EmployeeService implements IEmployeeDao{
	private EmployeeDao dao=new EmployeeDao();

	@Override
	public List<Student> findAll() {
		return dao.findAll();
	}

	@Override
	public Student findOne(int id) {
		return dao.findOne(id);
	}

	@Override
	public void update(Student student) {
		dao.update(student);
	}

	@Override
	public void delete(int id) {
		dao.delete(id);
	}

	@Override
	public void add(Student student) {
		dao.add(student);
	}

}

服务层接口:

package cn.itcast.service;

import java.util.List;

import cn.itcast.entity.Student;

public interface IEmployeeService {
	/*
	 * 查询全部学生信息
	 */
	public List<Student> findAll();
	
	/*
	 * 查询单个学生信息
	 */
	public Student findOne(int id);
	
	/*
	 * 修改单个学生信息
	 */
	public void update(Student student);
	
	/*
	 * 删除单个学生信息
	 */
	public void delete(int id);
	
	/*
	 * 增加单个学生信息
	 */
	public void add(Student student);
}

4.开发action

package cn.itcast.action;

import java.util.List;

import cn.itcast.entity.Student;
import cn.itcast.service.impl.EmployeeService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

public class EmployeeAction extends ActionSupport implements ModelDriven<Student>{
	private EmployeeService employeeService=new EmployeeService();
	private Student student=new Student();
	
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
	@Override
	public Student getModel() {
		// TODO Auto-generated method stub
		return student;
	}
	
	//添加学生
	public String add(){
		employeeService.add(student);
		return list();
	}
	
	//列表展示全部学生数据
	public String list(){
		List<Student> list=employeeService.findAll();
		ActionContext.getContext().getContextMap().put("list", list);
		return "list";
	}
	
	//进入修改学生信息页面
	public String updateView(){
		int id=student.getId();
		Student s=employeeService.findOne(id);
		//数据回显
		ValueStack vs=ActionContext.getContext().getValueStack();
		vs.pop();
		vs.push(s);
		return "updateView";
	}
	
	//修改学生信息
	public String update(){
		employeeService.update(student);
		return list();
	}
	
	//删除学生信息
	public String delete(){
		int id=student.getId();
		employeeService.delete(id);
		return list();
	}
	
}

5.配置struts.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="student" extends="struts-default">
		<action name="student_*" class="cn.itcast.action.EmployeeAction" method="{1}">
			<result name="list">/WEB-INF/list.jsp</result>
			<result name="updateView">/WEB-INF/update.jsp</result>
			
				<interceptor-ref name="defaultStack"/>
		          	<!-- 增加令牌拦截器 -->
		         	<interceptor-ref name="token">
		            <!-- 哪些方法被令牌拦截器拦截 -->
		            <param name="includeMethods">add</param>
        		</interceptor-ref>    
         <!-- 当表单重复提交转向的页面 -->
        	<result name="invalid.token" type="redirectAction">student_list</result>  	
			
		</action>
	</package>
</struts>    
6.jsp页面
6.1 add.jsp页面

<body>
    <s:form action="/student_add" method="post">
    <s:token></s:token>
    	<table>
    		<tr>
	    		<td>姓名</td>
	    		<td><s:textfield name="name"/></td>
	    	</tr>
	    	<tr>
	    		<td>年龄</td>
	    		<td><s:textfield name="age"/></td>
	    	</tr>
    		<tr>
	    		<td>性别</td>
	    		<td><s:textfield name="sex"/></td>
	    	</tr>
	    	<tr><td colspan="2"><s:submit value="保存"></s:submit></td></tr>
    	</table>
    </s:form>
  </body>
6.2 list.jsp页面
<body>
  <h1 align="center">学生信息查询</h1>
    <table border="1" width="80%" align="center" cellpadding="5"
		cellspacing="0">
    	<tr>
    		<td>序号</td>
    		<td>姓名</td>
    		<td>年龄</td>
    		<td>性别</td>
    		<td>修改</td>
    		<td>删除</td>
    	</tr>
    	<s:if test="#request.list!=null">
    		<s:iterator var="stu" value="#request.list" status="st">
    			<tr>
    				<td><s:property value="#st.count"/></td>
    				<td><s:property value="#stu.name"/></td>
    				<td><s:property value="#stu.age"/></td>
    				<td><s:property value="#stu.sex"/></td>
    				<td><s:a href="student_updateView?id=%{#stu.id}" >修改</s:a></td>
    				<td><s:a href="student_delete?id=%{#stu.id}" >删除</s:a></td>
    			</tr>
    		</s:iterator>
    	</s:if>
    	<s:else>
    		<td colspan="5">对不起没有你要显示的数据</td>
    	</s:else>
    </table>
  </body>
6.3 updatee.jsp页面
<body>
    <s:form action="/student_update" method="post">
    <s:hidden name="id"></s:hidden>
    	<table>
    		<tr>
	    		<td>姓名</td>
	    		<td><s:textfield name="name"/></td>
	    	</tr>
	    	<tr>
	    		<td>年龄</td>
	    		<td><s:textfield name="age"/></td>
	    	</tr>
    		<tr>
	    		<td>性别</td>
	    		<td><s:textfield name="sex"/></td>
	    	</tr>
	    	<tr><td colspan="2"><s:submit value="保存"></s:submit></td></tr>
    	</table>
    </s:form>
  </body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值