SpringMVC
一、知识点和用法案例
概念:
模型、视图、控制器组成,一种设计模式
作用:
解耦,便于开发和维护。
和Struts2区别:
机制不同,一个是Servlet,一个是Filter
拦截不同,一个是方法级,一个是类级别
一个配置更容易,一个配置更多
执行步骤:
1)导入SpringMVC依赖
2)Spring配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--包扫描-->
<context:component-scan base-package="com.hp.mvc"></context:component-scan>
<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--http://localhost:8080/mvc/index.jsp 找到/WEB-INF/pages/index.jsp-->
<!--前缀-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--配置拦截的URL请求路径 /**代表所有请求-->
<mvc:mapping path="/**"/>
<!--配置不拦截的请求路径-->
<mvc:exclude-mapping path="/**/login"/>
<mvc:exclude-mapping path="/**/*.css"/>
<mvc:exclude-mapping path="/**/*.js"/>
<mvc:exclude-mapping path="/**/*.png"/>
<mvc:exclude-mapping path="/**/*.jpg"/>
<!--配置class-->
<bean class="com.hp.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!--配置静态资源的处理servlet-->
<mvc:default-servlet-handler/>
<!--配置注解驱动-->
<!--注册类型转换器-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.hp.mvc.StudentConverter"/>
</list>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
</beans>
3) web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!--配置核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--springmvc的配置文件路径-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--启动顺序-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--处理所有资源-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--编码过滤器-->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4) 控制器
package com.hp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("user")
public class userController {
//方式一 通过参数名获得参数值
@PostMapping("/login")
public String login1(String username, String password,
Model model, HttpSession session){
System.out.println(username+"------"+password);
if ("zhangsan".equals(username)&&"123".equals(password)){
session.setAttribute("username",username);
//直接写页面的url,直接返回页面
/*return "successful";*/
//使用重定向,跳转到successful的方法
return "redirect:/mvc/success";
}
//保存数据
model.addAttribute("msg","账户或密码错误");
/* return "login";*/
//使用请求转发,跳转到login的方法
return "forward:/mvc/login";
}
//方式二 通过@RequestParam配置参数 @RequestParam(value = "表单的name",required=
// 是否必须,defaultValue=默认值)
/* @PostMapping("/login")
public String login2(@RequestParam("username") String name,
@RequestParam("password") String pwd,
@RequestParam(value="code",required=false,defaultValue="no code")){
System.out.println(name+"------"+pwd+"-----"+code);
if ("zhangsan".equals(name)&&"123".equals(pwd)){
return "successful";
}
return "login";
}*/
//方式三
/*@PostMapping("/login")
public String login3(User user){
System.out.println(user.getUsername()+"------"+user.getPassword());
if ("zhangsan".equals(user.getUsername())&&"123".equals(user.getPassword())){
return "successful";
}
return "login";
}*/
@ResponseBody
@RequestMapping("/ajax")
public List<User> ajax(@RequestBody User user){
System.out.println("收到了JSON对象:"+user);
//返回JSON集合给ajax
List<User> users= Arrays.asList(new User("user-1","123"),
new User("user-2","123"),
new User("user-3","123"));
return users;
}
}
SpringMVC的执行流程:
总结
以上就是MVC的所有知识点和用法。