SpringMVC基本使用

SpringMVC基本使用,第1张

SpringMVC基本使用

导入依赖

 
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
            org.springframework
            spring-webmvc
            5.2.5.RELEASE
        
        
        
            com.alibaba
            fastjson
            1.2.75
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.0
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.0
        
    
    
        
            
                src/main/java
                
                    ***.xml
                    **/*.properties
                
                true
            
        
    

springMVC的配置文件

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

web.xml

   
    
        dis
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:springMVC.xml
        
        
        1
    
    
        dis
        *.do
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

@RequestMapping() 处理请求

@RequestMapping(value="/ajax4.do",produces="text/plain;charset=utf-8")  设置编码 不然中文异常 produces属性设置响应中文数据

@RequestBody  处理ajax请求 放在请求方法前

@RequestParam 解决请求数据name和接收name不一致问题 放在接收方法形参前

Public ModelAndView doOther(@RequestParam("rName") String name,String age)

解决多次冲突访问路径

访问路径 /user/aaa.jsp   响应转发到此处 再次点击会出现404 原因是 多加个 user  /user/user/aaa.jsp

解决办法两种

1 在head中加入 base标签

href填入项目访问路径

2 在head加入拼接的访问路径

<%

String basePath=request.getScheme()+"://"+

request.getServerName()+":"+request.getServerPort()+

request.getContextPath()+"/";

%>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5716422.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存