Java from getting started to giving up learning notes spring MVC

Personal blog https://raptor1998.top/

Summary

Spring MVC is the son of spring and belongs to the web layer framework. It provides MVC (model view controller) architecture and components for developing flexible and loosely coupled web applications.

Introduction to spring MVC

Core architecture

  1. The browser sends the request to the front controller.
  2. After receiving the request, the front-end controller does not process it by itself, but entrusts it to other parsers for processing, as a unified access point for global process control. Handler mapping returns the handler to the front controller. Handler mapping maps the request to a HandlerExecutionChain object (including a handler handler (page controller) object and multiple HandlerInterceptor interceptor objects).
  3. The front-end controller calls the processor adapter to execute the Handler.
  4. The Handler adapter will execute the Handler according to the result of the adaptation. Call the corresponding function processing method of the processor and return a ModelAndView object.
  5. The View parser resolves the logical View name to a specific View.
  6. View will render based on the Model data passed in.
  7. The front controller responds to the user

Reference link

Understanding of spring MVC framework
Implementation principle and detailed explanation of spring MVC
Introduction to spring MVC

Spring MVC realizes the information management of class students

Catalog

configuration file

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- Packages using scan annotations, including subsets -->
    <context:component-scan base-package="controller"/>

    <!-- view resolver  -->
    <!--Resolve view logical name to: /WEB-INF/jsp/<viewName>.jsp -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

web.xml section

<filter>
		<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
		<url-pattern>*.do</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:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

controller

BjContriller

@Controller
@RequestMapping("/bj")
public class BjController {
	BjDao bjDao = new BjDao();

	@RequestMapping("/list")
	public String list(Model model) {

		try {
			List<Bj> bjList = bjDao.queryAll();
			model.addAttribute("bjList", bjList);
			return "bj/list";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/preadd")
	public String preadd() {
		return "bj/add";
	}

	@RequestMapping("/add")
	public String add(Bj bj, Model model) {

		try {
			bjDao.add(bj);
			model.addAttribute("info", "Add success");
			return "bj/add";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/edit")
	public String edit(int id, Model model) {

		try {
			Bj bj = bjDao.findBjById(id);
			model.addAttribute("bj", bj);
			return "bj/edit";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/update")
	public String update(Bj bj, Model model) {

		try {
			bjDao.update(bj);
			model.addAttribute("info", "Modified success");
			return "bj/edit";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable(value = "id") int id, Model model, HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		try {
			PrintWriter out = response.getWriter();
			if (bjDao.kongbj(id) != 0) {
				out.print("There are still students in this class, which cannot be deleted");
				return null;
			} else {
				bjDao.delete(id);
				return "redirect:/bj/list.do";
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	
}

StudentController

@Controller
@RequestMapping("/student")
public class StudentController {
	StudentDao studentDao = new StudentDao();
	BjDao bjDao = new BjDao();

	@RequestMapping("/list")
	public String list(Model model) {
		try {
			List<Student> studentList = studentDao.queryAll();
			model.addAttribute("studentList", studentList);
			return "student/list";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/preadd")
	public String preadd(Model model) {
		try {
			List<Bj> bjList=bjDao.queryAll();
			model.addAttribute("bjList", bjList);
			return "student/add";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		
	}

	@RequestMapping("/add")
	public String add(Model model, Student student, int bjid) {
		try {
			// System.out.println(student.getSname());
			// System.out.println(bjid);
			student.setBj(new Bj(bjid));
			studentDao.add(student);
			model.addAttribute("info", "Add success");
			return this.preadd(model);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/edit")
	public String edit(int id, Model model) {
		try {
			Student student = studentDao.findStudentById(id);
			model.addAttribute("student", student);
			List<Bj> bjList=bjDao.queryAll();
			model.addAttribute("bjList", bjList);
			return "student/edit";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/update")
	public String update(int id, String sno, String sname, String sex, int bjid, Model model) {

		try {
			Student student = new Student();
			student.setId(id);
			student.setSno(sno);
			student.setSname(sname);
			student.setSex(sex);
			student.setBj(new Bj(bjid));
			studentDao.update(student);
			model.addAttribute("info", "Modified success");
			return this.edit(id, model);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	@RequestMapping("/delete")
	public String delete(int id) {

		try {
			studentDao.delete(id);
			return "redirect:/student/list.do";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
}

github address

https://github.com/Raptor1998/JavaEE

152 original articles published, 69 praised, 380000 visitors+
Private letter follow

Tags: Spring xml JSP encoding

Posted on Sun, 12 Jan 2020 21:47:45 -0500 by Chadian22