MVC环境搭建
1.引入依赖
xml
<!-- 集成 spring 框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- 集成 web 框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
2. 修改项目web-inf下的web.xml
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>
<!--初始化CharacterEncodingFilter类时所需要的参数-->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!--响应的字符集也强制按照设置的encoding-->
<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>
<!--配置springmvc框架入口-->
<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 –>-->
<!-- <servlet>-->
<!-- <servlet-name>test</servlet-name>-->
<!-- <servlet-class>com.j3071.mvc.servlet.TestServlet01</servlet-class>-->
<!-- </servlet>-->
<!-- <!–建立 servlet 映射–>-->
<!-- <servlet-mapping>-->
<!-- <servlet-name>test</servlet-name>-->
<!-- <url-pattern>/test</url-pattern>-->
<!-- </servlet-mapping>-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.在resources目录下添加 springMVC-servlet.xml 内容如下
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">
<!--基于beanname的方式-->
<!-- <bean name="/testmvc" class="com.j3071.mvc.controller.BeanNameController"></bean>-->
<!--注解映射的方式-->
<context:component-scan base-package="com.j3071.mvc.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置解析物理地址的方式-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/><!--jsp的路径 如果在jsp包下 /jsp/-->
<property name="suffix" value=".jsp"/><!--后缀-->
</bean>
</beans>
4.编写controller
java
package com.j3071.mvc.controller;/*
* @Date:2023/11/16
* @Description: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");
//设置返回的数据 作用于req.setAttribute()一样
modelAndView.addObject("message","登陆成功");
return modelAndView;
}
/**
* 如何返回json数据
* @return
*/
@RequestMapping("/testJson")
@ResponseBody
public String m4(){
User user=new User();
user.setUserName("张三");
user.setPassWord("123456");
//方法返回值是String时 默认为返回的是页面 如果想返回String数据本身那么使用@ResponseBody注解
//return "test01";
return JSONObject.toJSONString(user);
}
}
5.Controller接收参数
java
@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");
//设置返回的数据 作用于req.setAttribute()一样
modelAndView.addObject("message","登陆成功");
return modelAndView;
}
}
6.处理form请求 返回ModelAndView
java
@RequestMapping("/testAnn2")
public ModelAndView m3(User user){
System.out.println(user.getUserName());
System.out.println(user.getPassWord());
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("test01");
//设置返回的数据 作用于req.setAttribute()一样
modelAndView.addObject("message","登陆成功");
return modelAndView;
}
7.处理ajax请求返回json数据 添加@ResponseBody
注解
java
/**
* 如何返回json数据
* @return
*/
@RequestMapping("/testJson")
@ResponseBody
public String m4(){
User user=new User();
user.setUserName("张三");
user.setPassWord("123456");
//方法返回值是String时 默认为返回的是页面 如果想返回String数据本身那么使用@ResponseBody注解
//return "test01";
return JSONObject.toJSONString(user);
}