• springMVC:modelandview,model,controller,参数传递


    转载:http://blog.csdn.net/wm5920/article/details/8173480

    1.web.xml 配置:

     

    copy
     

    <> ></> ></> > >> ></> ></> > ></> </> <> ></> ></> </>  

     

    这样,所有的.htm的请求,都会被DispatcherServlet处理;

    初始化 DispatcherServlet 时,该框架在 web 应用程序WEB-INF 目录中寻找一个名为[servlet-名称]-servlet.xml的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像上面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用<init-param>元素,手动指定配置文件的路径;

    dispatcher-servlet.xml 配置:

     

    copy
     

    <? = =?> < =  

    • =  
    •        =  
    • =  
    •        =  
    • =  
    •        =  
    • ="http://www.springframework.org/schema/beans  
    •             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    •   
    •         使Spring支持自动检测组件,如注解的Controller 
    •     -->  =/>  =  
    • =  
    •           =  
    • =   
    • >  


    2.spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void


    ModelAndView

     

     

       

    • (
    •  ModelAndView show1(HttpServletRequest request, 
    •            HttpServletResponse response)  Exception { 
    •        ModelAndView mav =  ModelAndView(); 
    •        mav.addObject(); 
    •         mav; 
    •    } 


    通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面 ,
    使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字。
    调用addObject()方法将值设置到一个名为ModelMap的类属性,ModelMap是LinkedHashMap的子类,
    具体请看类。

     


    Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。

    model.addAttribute("pojo", pojo);

    Map 

     

     

    @RequestMapping"/demo2/show"  Map<String, String> getMap() { 

    •         Map<String, String> map =  HashMap<String, String>(); 
    •         map.put(); 
    •         map.put(); 
    •          map; 
    •     } 

     


    在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。
    写例子时发现,key值包括 - . 时会有问题.

    View 可以返回pdf excel等,暂时没详细了解。

     


    String 指定返回的视图页面名称,结合设置的返回地址路径加上页面名称后缀即可访问到。

     

    注意:如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面。
    例如:

     

    @RequestMapping, method = RequestMethod.GET) 

    •  
    •  String helloWorld()  { 
    •  


    上面的结果会将文本"Hello World "直接写到http响应流。

     

    @RequestMapping"/welcome" public String welcomeHandler() { 

    •    


    对应的逻辑视图名为“center”,URL= prefix前缀+视图名称 +suffix后缀组成。

    void  如果返回值为空,则响应的视图页面对应为访问地址

     

    @RequestMapping"/welcome" public welcomeHandler() {} 
    此例对应的逻辑视图名为"welcome"。

    小结:

    1.使用 String 作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求 URL 绑定,具有很大的灵活性,而模型数据又可以通过 ModelMap 控制。
    2.使用void,map,Model 时,返回对应的逻辑视图名称真实url为:prefix前缀+视图名称 +suffix后缀组成。
    3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。

     

     

     

    Model model,HttpServletRequest request, ModelMap map声明变量

     


    request.getSession().setAttribute("test", "haiwei2Session");
    request.setAttribute("test", "haiwei1request"); 
    map.addAttribute("test", "haiweiModelMap");
    model.addAttribute("test", "haiweiModel");

    我通过${test}这个方式取值,优先取Model和ModelMap的,Model和ModelMap是同一个东西,谁最后赋值的就取谁的,然后是request,最后是从session中获取


    第一个Controller

    copy
     

    package import import @Controller public IndexController {  

    • ()  
    •      String index() {  
    •  ;  
    •     }  
    • }  

    @Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径(/index.htm),return "index"标记返回视图(index.jsp);

    注:如果@RequestMapping注解在类级别上,则表示一相对路径,在方法级别上,则标记访问的路径;

    从@RequestMapping注解标记的访问路径中获取参数:

    Spring MVC 支持RESTful风格的URL参数,如:

    copy
     

    @Controller public IndexController {  

    • ()  
    •  String index(<span style=></span>() String username) {  
    •         System.out.print(username);  
    •  ;  
    •     }  
    • }  

    @RequestMapping中定义访问页面的URL模版,使用{}传入页面参数,使用@PathVariable 获取传入参数,即可通过地址:http://localhost:8080/crm/index/tanqimin.htm 访问;

    根据不同的Web请求方法,映射到不同的处理方法:

    使用登陆页面作示例,定义两个方法分辨对使用GET请求和使用POST请求访问login.htm时的响应。可以使用处理GET请求的方法显示视图,使用POST请求的方法处理业务逻辑;

    copy
     

    @Controller public LoginController {  

    • (value = , method = RequestMethod.GET)  
    •      String login() {  
    •  ;  
    •     }  
    • (value = , method = RequestMethod.POST)  
    •      String login2(HttpServletRequest request) {  
    • ).trim();  
    •             System.out.println(username);  
    •  ;  
    •     }  
    • }  

    在视图页面,通过地址栏访问login.htm,是通过GET请求访问页面,因此,返回登陆表单视图login.jsp;当在登陆表单中使用POST请求提交数据时,则访问login2方法,处理登陆业务逻辑;

    防止重复提交数据,可以使用重定向视图:

    copy
     

    return  

    可以传入方法的参数类型:

     

     

    copy
     

    <strong>(value = , method = RequestMethod.POST)  

    •  String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {  
    • );  
    •     System.out.println(username);  
    •  ;  
    • }</strong>  


     

    可以传入HttpServletRequestHttpServletResponseHttpSession,值得注意的是,如果第一次访问页面,HttpSession没被创建,可能会出错;

    其中,String username = request.getParameter("username");可以转换为传入的参数:

     

    copy
     

    @RequestMapping, method = RequestMethod.POST)  

    •  String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,() String username) {  
    • );  
    •     System.out.println(username);  
    •  ;  
    • }  

     

    使用@RequestParam 注解获取GET请求或POST请求提交的参数;

    获取Cookie的值:使用@CookieValue :

    获取printwriter:

    可以直接在Controller的方法中传入PrintWriter对象,就可以在方法中使用:

     

    copy
     

    @RequestMapping, method = RequestMethod.POST)  

    •  String testParam(PrintWriter out, <span style=></span>() String username) {  
    •  ;  
    • }  

     

     

    获取表单中提交的值,并封装到POJO中,传入Controller的方法里:

    POJO如下(User.java):

     

    copy
     

    public User{  

    •       id;  
    •  String username;  
    •      String password;  
    • }  

     

     

    通过表单提交,直接可以把表单值封装到User对象中:

     

    copy
     

    @RequestMapping, method = RequestMethod.POST)  

    •  String testParam(PrintWriter out, User user) {  
    •  ;  
    • }  

     

     

    可以把对象,put 入获取的Map对象中,传到对应的视图:

     

     

    copy
     

    <strong>(value = , method = RequestMethod.POST)  

    •  String testParam(User user, Map model) {  
    • ,user);  
    •      ;  
    • }</strong>  

     

    在返回的view.jsp中,就可以根据key来获取user的值(通过EL表达式,${user }即可);

    Controller中方法的返回值:

    void:多数用于使用PrintWriter输出响应数据;

    String 类型:返回该String对应的View Name

    任意类型对象:

    返回ModelAndView

    自定义视图(JstlView,ExcelView):

    拦截器(Inteceptors):

     

     

    copy
     

    <strong>  MyInteceptor  HandlerInterceptor {  

    •       preHandle(HttpServletRequest request, HttpServletResponse response, Object o)   
    •  Exception {  
    •          ;  
    •   postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)   
    •  Exception {  
    •     }  
    •   afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)   
    •          Exception {  
    • }</strong>  

     

    拦截器需要实现HandleInterceptor接口,并实现其三个方法:

    preHandle:拦截器的前端,执行控制器之前所要处理的方法,通常用于权限控制、日志,其中,Object o表示下一个拦截器;

    postHandle:控制器的方法已经执行完毕,转换成视图之前的处理;

    afterCompletion:视图已处理完后执行的方法,通常用于释放资源;

    MVC的配置文件中,配置拦截器与需要拦截的URL

    copy
     

    <> >  =   

    •          =   
    • > </>  

     

    国际化:

    MVC配置文件中,配置国际化属性文件:

     

    copy
     

    < =  

    •     =  
    • => </>  

     

    那么,Spring就会在项目中搜索相关的国际化属性文件,如:message.propertiesmessage_zh_CN.properties

    VIEW中,引入Spring标签:<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>,使用<spring:message code="key" />调用,即可;

    如果一种语言,有多个语言文件,可以更改MVC配置文件为:

     

    copy
     

    < = =>  => > ></> ></> ></> > > </>  

  • 相关阅读:
    The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHa
    eclipse中的快捷键或者自动生成某个方法的操作
    mybatis和jadbc的对比
    1.解决数据库的错误:Can't connect to MySQL server on 'localhost' (10061) (2003);2.无法查看源码的问题;3.无法启动tomcat的问题解决
    2017年杭电计算机研究生复试笔试题目
    jfinal +oracle 排序问题
    oracle 找回被删除的数据
    oracle存储过程
    jfinal调用oracle存储过程
    本地连接oraclel --localhost可以连接ip连接不了解决办法
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5785610.html
Copyright © 2020-2023  润新知