初识 Spring(14)---(SpringMVC实战--构建学生管理系统(04))

初识 Spring(14)---(SpringMVC实战--构建学生管理系统(04))

首页部分添加班级功能制作(在上篇博客基础上继续)

classManager.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="${pageContext.request.contextPath}/static/js/admin-class.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="class-title">                    
			班级列表
		</div>
		<div id="add-div">
			<input type="button" id="add-button" value="添加" onclick="add()"/> // 添加班级功能
			<div id="form-div">
				<form action="" method="post">
					班级名称:<input type="text" name="classname"/>
					<input type="submit" value="提交"/>
				</form>
			</div>
		</div>
		<table cellspacing="0">
			<tr>
				<th>班级ID</th>
				<th>班级名称</th>
				<th>操作</th>
			</tr>
			<tr>
				<td>1</td>
				<td>一班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			<tr>
				<td>2</td>
				<td>二班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			<tr>
				<td>3</td>
				<td>三班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			<tr>
				<td>4</td>
				<td>四班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
		</table>
	</body>
</html>

点击 add 按钮,调用 add方法(在admin-class.js里

admin-class.js

$(function(){
	$('#form-div').hide();
});
function add(){
	$('#form-div').show();
}

当页面都加载完毕,才会执行该方法,即页面加载完毕,先隐藏添加班级页面,点击 add方法,使其显示

输出:

点击 添加 按钮

将信息添加到数据库

新建 ClassService.java

package com.neuedu.service;

import com.neuedu.po.TbClass;

public interface ClassService {

	public void save(TbClass tbClass);

}

新建 AdminServiceImpl.java

package com.neuedu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neuedu.mapper.TbClassMapper;
import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;

@Service
public class ClassServiceImpl implements ClassService {
	
	@Autowired
	private TbClassMapper tbClassMapper;

	@Override
	public void save(TbClass tbClass) {
		tbClassMapper.insertSelective(tbClass);		
	}

}

修改ClassController.java

package com.neuedu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

@Controller
@RequestMapping("/class")
public class ClassController {
	
	@Autowired
	private ClassService classService;
	
	@RequestMapping("/list")
	public String list(HttpSession session) {
		Object admin = session.getAttribute("admin");
		if(admin == null) {
			return "admin/login";
		}
		
		return "admin/classManager";
	} 
	
	@RequestMapping("/save")                        //新增代码
	public String save(TbClass tbClass) {
		classService.save(tbClass);
		return "admin/classManager";
	}                                              //新增代码
	
}

classManager.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="${pageContext.request.contextPath}/static/js/admin-class.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="class-title">
			班级列表
		</div>
		<div id="add-div">
			<input type="button" id="add-button" value="添加" onclick="add()"/>
			<div id="form-div">
				<form action="${pageContext.request.contextPath}/class/save" method="post">        //修改代码
					班级名称:<input type="text" name="classname"/>
					<input type="submit" value="提交"/>
				</form>
			</div>
		</div>
		<table cellspacing="0">
			<tr>
				<th>班级ID</th>
				<th>班级名称</th>
				<th>操作</th>
			</tr>
			<tr>
				<td>1</td>
				<td>一班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			<tr>
				<td>2</td>
				<td>二班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			<tr>
				<td>3</td>
				<td>三班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			<tr>
				<td>4</td>
				<td>四班</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
		</table>
	</body>
</html>

避免中文乱码:修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>course</display-name>
  
  <!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
     <filter>
     	<filter-name>characterEncoding</filter-name>        //新增代码
     	<filter-class> org.springframework.web.filter.CharacterEncodingFilter</filter-class>                                                //新增代码
     	<init-param>
     		<param-name>encoding</param-name>          //新增代码
     		<param-value>UTF-8</param-value>           //新增代码
     	</init-param>
     </filter>

	<filter-mapping>
		<filter-name>characterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  
</web-app>

输出:输入班级,点击添加,成功将信息注入数据库

上述列表是写死的,不能动态从数据库获取信息,修改代码

修改TbClassMapper.java

package com.neuedu.mapper;

import java.util.List;

import com.neuedu.po.TbClass;


public interface TbClassMapper {
    int deleteByPrimaryKey(Integer classid);

    int insert(TbClass record);

    int insertSelective(TbClass record);

    TbClass selectByPrimaryKey(Integer classid);

    int updateByPrimaryKeySelective(TbClass record);

    int updateByPrimaryKey(TbClass record);
    
    public List<TbClass> getList();   //新增代码

	
    
}

修改TbClassMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.neuedu.mapper.TbClassMapper" >
  <resultMap id="BaseResultMap" type="com.neuedu.po.TbClass" >
    <id column="classid" property="classid" jdbcType="INTEGER" />
    <result column="classname" property="classname" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    classid, classname
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tb_class
    where classid = #{classid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_class
    where classid = #{classid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.neuedu.po.TbClass" >
    insert into tb_class (classid, classname)
    values (#{classid,jdbcType=INTEGER}, #{classname,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.neuedu.po.TbClass" >
    insert into tb_class
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="classid != null" >
        classid,
      </if>
      <if test="classname != null" >
        classname,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="classid != null" >
        #{classid,jdbcType=INTEGER},
      </if>
      <if test="classname != null" >
        #{classname,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.neuedu.po.TbClass" >
    update tb_class
    <set >
      <if test="classname != null" >
        classname = #{classname,jdbcType=VARCHAR},
      </if>
    </set>
    where classid = #{classid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.neuedu.po.TbClass" >
    update tb_class
    set classname = #{classname,jdbcType=VARCHAR}
    where classid = #{classid,jdbcType=INTEGER}
  </update>
  
  <select id="getList" resultType="TbClass">    //新增代码
  	select * from tb_class
  </select>                                     //新增代码
</mapper>

修改 ClassService.java

package com.neuedu.service;

import com.neuedu.po.TbClass;
import java.util.List;

public interface ClassService {

	public void save(TbClass tbClass);
	public List<TbClass> getList();           //新增代码


	
}

修改ClassServiceImpl.java

package com.neuedu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neuedu.mapper.TbClassMapper;
import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;

@Service
public class ClassServiceImpl implements ClassService {
	
	@Autowired
	private TbClassMapper tbClassMapper;

	@Override
	public void save(TbClass tbClass) {
		tbClassMapper.insertSelective(tbClass);		
	}

	@Override
	public List<TbClass> getList() {              //新增代码
		
		return tbClassMapper.getList();            //新增代码
	}

}

修改ClassController.java

package com.neuedu.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.po.TbClass;
import com.neuedu.service.ClassService;


@Controller
@RequestMapping("/class")
public class ClassController {
	
	@Autowired
	private ClassService classService;  //新增代码
	
	@RequestMapping("/list")
	public String list(HttpSession session,Model model) {  //新增代码
		Object admin = session.getAttribute("admin");    //新增代码
		if(admin == null) {
			return "admin/login";
		}
		
		System.out.println(model.getClass().getName());
		
		List<TbClass> list = classService.getList();
		model.addAttribute("list",list);
		
		return "admin/classManager";
	} 
	/*转发和重定向:
	redirect:重定向到后面的路径里,不会再加前缀和后缀
	forward:转发到后面的路径里,不会再加前缀和后缀
	*/
	
	@RequestMapping("/save")              //新增代码
	public String save(HttpSession session,TbClass tbClass) {    //新增代码
		Object admin = session.getAttribute("admin");
		if(admin == null) {
			return "admin/login";         //新增代码
		}
		
		classService.save(tbClass);       //新增代码
		
		return "forward:/class/list";     //新增代码
	}
	
}

修改 classManager.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>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="${pageContext.request.contextPath}/static/js/admin-class.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="class-title">
			班级列表
		</div>
		<div id="add-div">
			<input type="button" id="add-button" value="添加" onclick="add()"/>
			<div id="form-div">
				<form action="${pageContext.request.contextPath}/class/save" method="post">
					班级名称:<input type="text" name="classname"/>
					<input type="submit" value="提交"/>
				</form>
			</div>
		</div>
		<table cellspacing="0">
			<tr>
				<th>班级ID</th>
				<th>班级名称</th>
				<th>操作</th>
			</tr>
			<c:forEach items="${list }" var="c">     //修改代码
				<tr>
				<td>${c.classid }</td>
				<td>${c.classname }</td>
				<td>
					<a href="">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			</c:forEach>                              //修改代码
			
			
		</table>
	</body>
</html>

输出:输入内容--点击添加---成功从数据库导入页面

猜你喜欢

转载自blog.csdn.net/jianghao233/article/details/81623963
今日推荐