SpringMVC是一个基于Java的、扩展性强的轻量级Web框架,它是Spring框架的一部分,用于构建Web应用程序。SpringMVC提供了一种松散耦合的设计方法,通过分离应用程序的不同组件(模型、视图和控制器)来提高可维护性和灵活性。
以下是SpringMVC的一些主要特点和概念:
模型-视图-控制器(MVC)架构: SpringMVC采用经典的MVC设计模式,将应用程序分为模型(Model,处理业务逻辑)、视图(View,负责展示数据)和控制器(Controller,或Handler,处理用户请求和返回响应)三个组件。
前端控制器(DispatcherServlet): SpringMVC采用前端控制器模式,所有的请求都由一个单一的控制器(DispatcherServlet)来处理,它负责将请求分发给合适的处理器(Handler)进行处理。
处理器映射器(HandlerMapping): 处理器映射器负责将请求映射到合适的处理器(Handler)上,根据请求的URL来确定使用哪个Handler处理请求。
处理器适配器(HandlerAdatper): 处理器适配器根据 Handler 规则执行不同类型的 Handler
视图解析器(ViewResolver): 视图解析器负责将逻辑视图名解析为具体的视图对象,这样控制器就可以返回逻辑视图名,而不需要关心具体的视图对象。
后端控制器(处理器 Handler):接收表单数据、超链接数据、调用service进行相关的操作
1.web.xml 配置前端控制器
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
2.springmvc.xml 配置处理器适配器、处理器映射器以及视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">bean>
<mvc:annotation-driven>mvc:annotation-driven>
<context:component-scan base-package="com.kdx.controller">context:component-scan>
3.实体类Student
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
private Integer age;
public Student() {
}
public Student(Integer id, String name, String sex, Integer age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4.开发Handler
@Controller
public class StudentController1 {
@RequestMapping("/queryStudent")
public ModelAndView handleRequest() throws Exception {
Student student1 = new Student(1,"kong","男",18);
Student student2 = new Student(2,"sun","男",18);
Student student3 = new Student(3,"wu","男",18);
List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("studentList",studentList);
modelAndView.setViewName("/WEB-INF/jsp/studentList.jsp");
return modelAndView;
}
}
5.开发视图
<c:forEach items="${requestScope.studentList}" var="s">
${s.id}
${s.name}
${s.sex}
${s.age}
<hr/>
c:forEach>
更改 springmvc.xml
<!--配置处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--配置Handler-->
<bean name="/queryStudent.action" class="com.kdx.controller.StudentController"></bean>
<!--配置处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
开发Handler:
public class StudentController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
Student student1 = new Student(1,"kong","男",18);
Student student2 = new Student(2,"sun","男",18);
Student student3 = new Student(3,"wu","男",18);
List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("studentList",studentList);
modelAndView.setViewName("/WEB-INF/jsp/studentList.jsp");
return modelAndView;
}
}