MVC环境搭建

MVC环境搭建

1.引入依赖

  
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>5.2.13.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>5.2.13.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.13.RELEASEversion>
        dependency>

2. 修改项目web-inf下的web.xml

<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>
    
    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>


    <servlet>
        
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springMVC-servlet.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    
    
    
    
    
    







    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>



web-app>

3.在resources目录下添加 springMVC-servlet.xml 内容如下


<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;/*
 * @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接收参数

@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

  @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注解

  /**
     * 如何返回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);
    }

你可能感兴趣的:(SpringMVC,java,mvc,java,spring)