基于Result的页面数据交互

首先导入前端的文件,但是这里需要注意,当时配置时,将所有的请求都拦截了下来且令其归SpringMVC管

package com.itjh.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    //哪些请求归属于SpringMVC管
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    protected  Filter[] getFilter(){
        CharacterEncodingFilter filter=new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}

所以需要写一个用来放行的配置类,并且让SpringMVC配置类来加载它,看看放行的配置类中放什么呢:

package com.itjh.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class SpringMvcSupportConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //前面位置的包的请求访问被拦截时,会自动进入后面的包里面找数据
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
    }
}

SpringMVC配置类:看注释

package com.itjh.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
//扫描控制类和放行前端需求SpringMvcSupportConfig
@ComponentScan({"com.itjh.contorller","com.itjh.config"})
//转换器,这样就可以json的数据转换了
@EnableWebMvc
public class SpringMvcConfig {
}
  • 前端页面中填写的路径尽量写直接路径:展示一个小方法
  • 其中路径最后面的一小节是自己起的模块名(即控制类上面注解的名字)
    看第二个代码
methods: {
                //列表
                getAll() {
                    //发送ajax请求
                    axios.get("http://localhost:8080/ssm01/books").then((res)=>{
                        this.dataList = res.data.data;
                    });
                },
package com.itjh.contorller;

import com.itjh.excption.BusinessException;
import com.itjh.excption.SystemException;
import com.itjh.pojo.Book;
import com.itjh.service.ServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/books")
public class serv {}

最后启动了TomCat之后,在页面上访问时要把包名也写在路径前面:即

http://localhost:8080/ssm01/pages/books.html

基于Result的页面数据交互_第1张图片

你可能感兴趣的:(交互,servlet,前端)