spring MVC要注意的地方

简介:

spring MVC要注意的地方:

控制器代码如下:

Java代码   收藏代码
  1. package com.mvc.jn.controller;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.ui.Model;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestMethod;  
  8. import org.springframework.web.bind.annotation.RequestParam;  
  9. import org.springframework.web.servlet.ModelAndView;  
  10.   
  11. @org.springframework.stereotype.Controller  
  12. public class HelloWorldController {  
  13.   
  14.     // http://localhost:8088/springMVCannotations/hello2  
  15.     @RequestMapping(value = "/hello2", method = RequestMethod.GET)  
  16.     public String sayHello2(  
  17.             @RequestParam(value = "name", required = false) String username,  
  18.             Map<String, Object> map) {  
  19.         map.put("message1", username);  
  20.         System.out.println("my name:" + username);  
  21.         return "hello22";  
  22.     }  
  23.   
  24.     // http://localhost:8088/springMVCannotations/hello3  
  25.     @RequestMapping(value = "/hello3", method = RequestMethod.GET)  
  26.     public ModelAndView sayHello3(  
  27.             @RequestParam(value = "name", required = false) String username) {  
  28.         System.out.println("my name:" + username);  
  29.         ModelAndView mav = new ModelAndView("hello33");  
  30.         mav.getModel().put("message1", username);  
  31.         return mav;  
  32.     }  
  33.   
  34.     // http://localhost:8088/springMVCannotations/hello44?name=whuang  
  35.     @RequestMapping(value = "/hello44", method = RequestMethod.GET)  
  36.     // 与网上说的不一致,网上说方法名就是试图  
  37.     public Model hello4(  
  38.             @RequestParam(value = "name", required = false) String username,  
  39.             Model model) {  
  40.         System.out.println("my name:" + username);  
  41.         model.addAttribute("message1", username);  
  42.         return model;  
  43.     }  
  44.   
  45.     @RequestMapping(value = "/hello555", method = RequestMethod.GET)  
  46.     public Model hello5(String username, Model model) {  
  47.         System.out.println("my name:" + username);  
  48.         model.addAttribute("message1", username);  
  49.         return model;  
  50.     }  
  51.   
  52. }  

 ViewResolver配置如下:

Xml代码   收藏代码
  1. <!-- ViewResolver -->  
  2.     <bean  
  3.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  4.         <property name="viewClass"  
  5.             value="org.springframework.web.servlet.view.JstlView" />  
  6.         <property name="prefix" value="/WEB-INF/jsp/" />  
  7.         <property name="suffix" value=".jsp" />  
  8.     </bean>  

 问题1:调用控制器中的hello4 方法后会返回那个视图?

按照网上的说法,会返回“hello4”(方法名),如下图



 但是经过我反复测试,发现它返回的视图是“
hello44”,即@RequestMapping 指定的value。

 

问题2:访问方法hello5时,必须传参数username 吗?

答:不是必须的。

什么情况下是必须的呢?

当有注解@RequestParam(value = "username")时才是必须的,若不传该参数将报错.

url 是“http://localhost:8088/springMVCannotations/hello555?username=ccc”时,会自动设置username的值

 

问题3:spring MVC与hibernate4集成时报错:No Session found for current thread

详情请参阅我的另一篇博客:http://hw1287789687.iteye.com/blog/1949852

 

问题4:启动tomcat时报错:Java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

原因是:pom.xml中缺少:

Xml代码   收藏代码
  1. <dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-web</artifactId>  
  4.             <version>3.2.3.RELEASE</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring-webmvc</artifactId>  
  9.             <version>3.2.3.RELEASE</version>  
  10.         </dependency>  

 

 

问题5:spring MVC 使用bean接收参数时如何传参?

比如查询时,controller 把参数注入到bean中,

Java代码   收藏代码
  1. @RequestMapping(value = "/show")  
  2.     public String show(Model model,User user,UserView view,HttpSession session) throws CloneNotSupportedException {  
  3.         if(!ValueWidget.isNullOrEmpty(view.getPageFlag())&&view.getPageFlag().equals(Constant2.PAGEFLAG_NOT_QUERY)){  
  4.             System.out.println("不是查询");  
  5.             user=(User)session.getAttribute("user2");  
  6.             try {  
  7.                 BeanUtils.copyProperties(view, user);  
  8.             } catch (IllegalAccessException e) {  
  9.                 e.printStackTrace();  
  10.             } catch (InvocationTargetException e) {  
  11.                 e.printStackTrace();  
  12.             }  
  13.         }else{//查询  
  14.             System.out.println("是查询");  
  15.             session.setAttribute("user2", user);  
  16.         }  
  17.         PageAssistant.paging(user,true,view, userDao);  
  18.           
  19.         model.addAttribute("view", view);  
  20.         model.addAttribute("currentTime", TimeHWUtil.getCurrentTimestamp().getTime());  
  21.           
  22.           
  23.         return "user/show";  
  24.     }  

 方法show()的参数user会保存请求要素,那么在页面上是如何编写表单控件name的呢?

Java代码   收藏代码
  1. <Li>                      
  2.                       <label>用户名:</label><input type="text"  name="user.username" value="${view.username }"  />  
  3.                   </Li>  
  4.                   <Li>  
  5.                           <label>真实姓名:</label><input type="text" name="user.name" value="${view.name }" />  
  6.                   </Li>  
  7.                    

 上述代码是不对的,应该是:

  <Li>                    

                        <label>用户名:</label><input type="text"  name="username" value="${view.username }"  />

                    </Li>

                    <Li>

                            <label>真实姓名:</label><input type="text" name="name" value="${view.name }" />

                    </Li>

相关文章
|
1月前
|
缓存 前端开发 Java
Spring MVC 面试题及答案整理,最新面试题
Spring MVC 面试题及答案整理,最新面试题
90 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
1月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
26 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
42 1
|
13天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
13天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
13天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
29 3
|
23天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
29天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
69 0