MVC环境搭建
1.引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
2. 修改项目web-inf下的web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</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:springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.在resources目录下添加 springMVC-servlet.xml 内容如下
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.j3071.mvc.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4.编写controller
package com.j3071.mvc.controller;
import com.alibaba.fastjson.JSONObject;
import com.j3071.mvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class AnnController {
@RequestMapping("/testAnn")
public ModelAndView m1(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
return modelAndView;
}
@RequestMapping("/testAnn1")
public ModelAndView m2(@RequestParam("userName") String userName,
@RequestParam("passWord") String passWord){
System.out.println(userName);
System.out.println(passWord);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
return modelAndView;
}
@RequestMapping("/testAnn2")
public ModelAndView m3(User user){
System.out.println(user.getUserName());
System.out.println(user.getPassWord());
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
modelAndView.addObject("message","登陆成功");
return modelAndView;
}
@RequestMapping("/testJson")
@ResponseBody
public String m4(){
User user=new User();
user.setUserName("张三");
user.setPassWord("123456");
return JSONObject.toJSONString(user);
}
}
5.Controller接收参数
@Controller
public class AnnController {
@RequestMapping("/testAnn1")
public ModelAndView m2(@RequestParam("userName") String userName,
@RequestParam("passWord") String passWord){
System.out.println(userName);
System.out.println(passWord);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
return modelAndView;
}
@RequestMapping("/testAnn2")
public ModelAndView m3(User user){
System.out.println(user.getUserName());
System.out.println(user.getPassWord());
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
modelAndView.addObject("message","登陆成功");
return modelAndView;
}
}
6.处理form请求 返回ModelAndView
@RequestMapping("/testAnn2")
public ModelAndView m3(User user){
System.out.println(user.getUserName());
System.out.println(user.getPassWord());
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
modelAndView.addObject("message","登陆成功");
return modelAndView;
}
7.处理ajax请求返回json数据 添加@ResponseBody
注解
@RequestMapping("/testJson")
@ResponseBody
public String m4(){
User user=new User();
user.setUserName("张三");
user.setPassWord("123456");
return JSONObject.toJSONString(user);
}