spring MVC要注意的地方:
控制器代码如下:
package com.mvc.jn.controller; import java.util.Map; import org.springframework.ui.Model; 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.servlet.ModelAndView; @org.springframework.stereotype.Controller public class HelloWorldController { // http://localhost:8088/springMVCannotations/hello2 @RequestMapping(value = "/hello2", method = RequestMethod.GET) public String sayHello2( @RequestParam(value = "name", required = false) String username, Map<String, Object> map) { map.put("message1", username); System.out.println("my name:" + username); return "hello22"; } // http://localhost:8088/springMVCannotations/hello3 @RequestMapping(value = "/hello3", method = RequestMethod.GET) public ModelAndView sayHello3( @RequestParam(value = "name", required = false) String username) { System.out.println("my name:" + username); ModelAndView mav = new ModelAndView("hello33"); mav.getModel().put("message1", username); return mav; } // http://localhost:8088/springMVCannotations/hello44?name=whuang @RequestMapping(value = "/hello44", method = RequestMethod.GET) // 与网上说的不一致,网上说方法名就是试图 public Model hello4( @RequestParam(value = "name", required = false) String username, Model model) { System.out.println("my name:" + username); model.addAttribute("message1", username); return model; } @RequestMapping(value = "/hello555", method = RequestMethod.GET) public Model hello5(String username, Model model) { System.out.println("my name:" + username); model.addAttribute("message1", username); return model; } }
ViewResolver配置如下:
<!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </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中缺少:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.3.RELEASE</version> </dependency>
问题5:spring MVC 使用bean接收参数时如何传参?
比如查询时,controller 把参数注入到bean中,
@RequestMapping(value = "/show") public String show(Model model,User user,UserView view,HttpSession session) throws CloneNotSupportedException { if(!ValueWidget.isNullOrEmpty(view.getPageFlag())&&view.getPageFlag().equals(Constant2.PAGEFLAG_NOT_QUERY)){ System.out.println("不是查询"); user=(User)session.getAttribute("user2"); try { BeanUtils.copyProperties(view, user); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }else{//查询 System.out.println("是查询"); session.setAttribute("user2", user); } PageAssistant.paging(user,true,view, userDao); model.addAttribute("view", view); model.addAttribute("currentTime", TimeHWUtil.getCurrentTimestamp().getTime()); return "user/show"; }
方法show()的参数user会保存请求要素,那么在页面上是如何编写表单控件name的呢?
<Li> <label>用户名:</label><input type="text" name="user.username" value="${view.username }" /> </Li> <Li> <label>真实姓名:</label><input type="text" name="user.name" value="${view.name }" /> </Li>
上述代码是不对的,应该是:
<Li>
<label>用户名:</label><input type="text" name="username" value="${view.username }" />
</Li>
<Li>
<label>真实姓名:</label><input type="text" name="name" value="${view.name }" />
</Li>
注意:spring MVC 的依赖注入与struts2 是不同的.
spring MVC 的学习资料见附件spring MVC学习笔记.zip