关于SpringMVC的XML配置步骤

1).配置前端控制



    
        /login.jsp
    

    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
    
    
        springmvc
        /
    
​
​

2).根据前端控制器里面的init- param里面的classpath配置spring-mvc.xml





    
    





  • 在里面配置视图解析器

  • 静态资源的放行

  • json结果类型的自动结果类型转换,一句代码搞定,不需要记住那么复杂的xml配置

3).写controller层代码

package com.qiao.controller;
​
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
​
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
​
@Controller
public class Param {
    @RequestMapping("/param1")
    public void method1(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws Exception{
        req.setAttribute("data1","request域存的内容");
        session.setAttribute("data2","session域存的内容");
        req.getRequestDispatcher("/WEB-INF/jsp/result.jsp").forward(req,resp);
    }
    @RequestMapping("/param2")
    public String method2(Model model , ModelMap modelMap){
        model.addAttribute("data1","data1内容");
        modelMap.addAttribute("data2","data2内容");
        return "result2";
    }
​
    @RequestMapping(value = "/param3",method = RequestMethod.GET,produces = {"text/json;charset=utf-8"})
    @ResponseBody
    public String method3(@RequestParam(value = "id",defaultValue ="66") Integer UserId, String name){
        return UserId+":"+name;
    }
​
}
​

关于mvc:view-controller的用法

关于SpringMVC的XML配置步骤_第1张图片

你可能感兴趣的:(spring全家桶,xml,mvc,spring)