SpringMVC项目实例代码(基础版),执行流程和三层架构类的代码

1.调用WEB-INF里面的index.jsp

在得到配置后这个java代码可以直接得到浏览器发来的请求"system"

package com.dmc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexConrtoller {
	//java代码可以直接接收浏览器的请求,在得到注解后
	@RequestMapping("/system")//接收来自浏览器的直接访问
	//去后台首页
	public String goIndex(){
		return "WEB-INF/system/index";//跳转到这个jsp页面,这个字符串经过视图解析器,自动变成jsp的全名直接访问jsp文件
	}
	
}

2.发送请求

代码中有一个src=“system/jobs/show” 用来发送请求

<body>
	<div class="container-fluid" style="margin-top: 60px;">
		<div class="row">
			<!-- 内容展示页 -->
			<div class="main_page">									 <!-- 加载本jsp文件后自动发送这个请求 -->
				<iframe scrolling="no" id="mainContent" name="mainContent" src="system/jobs/show" frameborder="0" src="" style="min-height:600px;width:100%;height:100%;"></iframe>
			</div>													 <!-- 加载本jsp文件后自动发送这个请求 -->
		</div>
	</div>
</body>

3.接收请求并处理

上面传的请求到这里的就是"system/jobs/show" ,代码匹配到相应的请求之后做相应的逻辑处理:调用goIndex()方法

package com.dmc.controller;

import java.util.List;

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.dmc.domain.Jobs;
import com.dmc.service.SystemIJobsService;

@Controller
@RequestMapping("/system/jobs")
public class SystemJobsController {
	
	@Autowired
	public SystemIJobsService service;
	
	@RequestMapping("/show")//到这里的就是请求"system/jobs/show"
	public String goIndex(Model mo){
		List<Jobs> jobs = service.findAll();
		mo.addAttribute("jobs", jobs);
		return "WEB-INF/system/jobs";//跳转到这个jsp页面
	}
	
	@RequestMapping("/del")//到这里的就是请求"system/jobs/del"
	public String delete(Integer id){
		service.delete(id);
		return "redirect:show";//跳转到这个jsp页面
	}
}

4.上一个代码在完成逻辑处理后发送请求"WEB-INF/system/jobs"经过视图解析器后变成"/WEB-INF/system/jobs.jsp",绑定值、传递值后直接访问这个jsp页面

<c:forEach items="${jobs}" var="j">
		<tr>
			<th>#${ j.id }</th>
			<th>${ j.title }</th>
			<th>${ j.address }</th>
			<th>${ j.jobnum }</th>
			<th>${ j.treatment }</th>
			<c:if test="${j.isenabled }" var="t">
				<th><span class="glyphicon glyphicon-ok" aria-hidden="ture"></span></th>
			</c:if>
			<c:if test="${!t }">
				<th><span class="glyphicon glyphicon-remove" aria-hidden="ture"></span></th>
			</c:if>
			<th>[${ j.inputdate }]</th>
			<th>
				<a href="jobs_add.html" class="btn-default tableA"><span class="glyphicon glyphicon-plus" aria-hidden="true">添加</span></a>
				<a href="jobs_edit.html" class="btn-default tableA"><span class="glyphicon glyphicon-pencil" aria-hidden="true">修改</span></a>
				<a href="system/jobs/del?id=${ j.id }" class="btn-default tableA"><span class="glyphicon glyphicon-trash" aria-hidden="true">删除</span></a>
			</th>
		</tr>
		</c:forEach>

5. 步骤3的逻辑处理List jobs = service.findAll();

	逻辑处理是就要注意些注解了,注解写在实现类和controller类里面,接口不写注解。
	自动注入的注解写在如下形式代码
@Repository
public class SystemJobsDaoImpl implements SystemIJobsDao{
	
	@Autowired
	public JdbcTemplate temple;

	@Override
	public List<Jobs> findAll() {
		return temple.query("select * from jobs", new BeanPropertyRowMapper<Jobs>(Jobs.class));
	}

	@Override
	public void delete(Integer id) {
		temple.update("delete from jobs where id = ? " , id);
	}
}

6.步骤3的绑定值mo.addAttribute(“jobs”, jobs);

获取方式
		<th>#${ j.id }</th>
		<th>${ j.title }</th>
		<th>${ j.address }</th>
		<th>${ j.jobnum }</th>
		<th>${ j.treatment }</th>

7.三层架构中的代码

1.controller层

package com.dmc.controller;

import java.util.List;

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.dmc.domain.Citys;
import com.dmc.domain.Jobs;
import com.dmc.service.SystemICityService;
import com.dmc.service.SystemIJobsService;
import com.dmc.util.PageBean;

@Controller
@RequestMapping("/system/jobs")
public class SystemJobsController {
	
	@Autowired
	public SystemIJobsService service;
	
	@Autowired
	public SystemICityService cityService;
	
	@RequestMapping("/show")
	public String goIndex(Integer localPage , Model mo){
		//List<Jobs> jobs = service.findAll();
		PageBean<Jobs> pageBean = service.page(localPage);
		mo.addAttribute("pageBean", pageBean);
		return "WEB-INF/system/jobs";//跳转到这个jsp页面
	}
	
	@RequestMapping("/del")
	public String delete(Integer id){
		service.delete(id);
		return "redirect:show";//跳转到这个jsp页面
	}
	
	
	//city的方法调用
	@RequestMapping("/toAdd")
	public String toAdd(Model mo){
		List<Citys> citys = cityService.findAll();
		mo.addAttribute("citys",citys);
		return "WEB-INF/system/jobs_add";
	}
	
	//调用jobs方法
	@RequestMapping("/add")
	public String add(Jobs j){
		service.add(j);
		return "redirect:show";//跳转到这个jsp页面
	}
	
	@RequestMapping("/update1")
	public String update1(Integer id , Model mo){
		//这里把单个查询设定成了update
		Jobs j = service.update1(id);//调用的是单个查询的功能,但写成了update
		mo.addAttribute("job1",j);
		
		List<Citys> citys = cityService.findAll();
		mo.addAttribute("citys",citys);
		
		return "WEB-INF/system/jobs_edit";//跳转到这个jsp页面
	}
	
	@RequestMapping("/update2")
	public String update2(Jobs job){
		System.out.println(job);
		service.updateGo(job);
		return "redirect:show";//跳转到这个jsp页面
	}
}

2.service层实现类

package com.dmc.service.impl;

import java.util.List;

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

import com.dmc.dao.SystemIJobsDao;
import com.dmc.domain.Jobs;
import com.dmc.service.SystemIJobsService;
import com.dmc.util.PageBean;
@Service
public class SystemJobsServiceImpl implements SystemIJobsService{

	@Autowired
	public SystemIJobsDao dao;
	
	@Override
	public List<Jobs> findAll() {
		return dao.findAll();
	}

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

	@Override
	public void add(Jobs j) {
		dao.add(j);
	}

	@Override
	public Jobs update1(Integer id) {
		return dao.update1(id);
	}

	@Override
	public void updateGo(Jobs job) {
		dao.updateGo(job);
	}

	@Override
	public PageBean<Jobs> page(Integer localPage) {
		Integer totalNum = dao.findTotalNum();
		if(localPage == null){
			localPage = 1;
		}
		PageBean<Jobs> pageBean = new PageBean<>(localPage,totalNum);

		List<Jobs> l = dao.findList((pageBean.getLocalPage()-1)*pageBean.getPageSize(),pageBean.getPageSize());
		pageBean.setList(l);
		return pageBean;
	}
}

3.dao层实现类

package com.dmc.dao.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.dmc.dao.SystemIJobsDao;
import com.dmc.domain.Jobs;
@Repository
public class SystemJobsDaoImpl implements SystemIJobsDao{
	
	@Autowired
	public JdbcTemplate temple;

	@Override
	public List<Jobs> findAll() {
		return temple.query("select * from view_jobs_citys", new BeanPropertyRowMapper<Jobs>(Jobs.class));
	}

	@Override
	public void delete(Integer id) {
		temple.update("delete from jobs where id = ? " , id);
	}

	@Override
	public void add(Jobs j) {
		String sql = "insert into jobs(title,cid,jobnum,treatment,describes,requires,htmlurl,positiontype,isenabled,inputdate) values(?,?,?,?,?,?,?,?,?,?)";
		temple.update(sql,j.getTitle(),j.getCid(),j.getJobnum(),j.getTreatment(),j.getDescribes(),j.getRequires(),j.getHtmlurl(),j.getPositiontype(),j.getIsenabled(),j.getInputdate());
	}
	
	//这里是单个查询,写成了update
	@Override
	public Jobs update1(Integer id) {
		return temple.queryForObject("select * from jobs where id = ?", new BeanPropertyRowMapper<Jobs>(Jobs.class),id);
	}

	@Override
	public void updateGo(Jobs jobs) {
		String sql="update jobs set title=? ,cid = ?,jobnum = ?, treatment=?,describes=?,requires=?,htmlurl=?,positiontype=?,isenabled=?,inputdate=? where id = ?";
		temple.update(sql, jobs.getTitle(),jobs.getCid(),jobs.getJobnum(),jobs.getTreatment(),jobs.getDescribes(),jobs.getRequires(),jobs.getHtmlurl(),jobs.getPositiontype(),jobs.getIsenabled(),jobs.getInputdate(),jobs.getId());
	}

	@Override
	public Integer findTotalNum() {
		Integer totalNum = temple.queryForObject("select count(id) from jobs", Integer.class);
		return totalNum;
	}

	@Override
	public List<Jobs> findList(int i, Integer pageSize) {
		return temple.query("select * from jobs limit ? , ? ", new BeanPropertyRowMapper<Jobs>(Jobs.class) , i , pageSize);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值