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


		
			#${ j.id }
			${ j.title }
			${ j.address }
			${ j.jobnum }
			${ j.treatment }
			
				
			
			
				
			
			[${ j.inputdate }]
			
				
				
				
			
		
		

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

获取方式
		#${ j.id }
		${ j.title }
		${ j.address }
		${ j.jobnum }
		${ j.treatment }

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 = 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);
	}
}

你可能感兴趣的:(Java,Spring阶段)