[email protected]
用于指示Spring类的实例是一个控制器。Controller接口的实现类只能处理一个单一请求动作,而@Controller注解的控制器可以支持同时处理多个请求动作,更加灵活。Spring使用扫描机制查找应用程序中所有基于注解的控制器类。分发处理器会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping注解,而使用@RequestMapping注解的方法才是真正处理请求的处理器。为了保证能找到控制器,需要完成两件事情:
在Spring MVC的配置文件中以你如spring-context
使用<context:component-scan/>元素,该元素的功能为:启动包扫描功能,以便注册带有@Controller,@Service,@repository,@Component等注解的类成为SPring的Bean
<context:component-scan base-package="包路径">
应该 将所有控制器类都在基本包下,并且指定扫描该包
http://localhost:8080/AnnotationTest/haha
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping(value="/haha") public String hello(Model model) { model.addAttribute("message", "腾飞新星"); return "haha"; } }
发送haha请求,将model的message赋值"腾飞新星",再返回给haha.jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
prefix视图前缀,suffix视图后缀
[email protected]
用于指示Spring用哪一个类或者哪一个方法来处理请求动作。
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value="/user") public class UserController { @RequestMapping(value="/register") public String register() { return "register"; } @RequestMapping(value="/login") public String login() { return "login"; } }
由于UserController前面加上@RequestMapping(value="/user"),所以相关的请求都要加上/user
创建register.jsp和login.jsp
http://localhost:8080/AnnotationTest/user/register
http://localhost:8080/AnnotationTest/user/login
即可访问成功
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloController { @RequestMapping(value="/haha",method=RequestMethod.GET,params="myParam=nidaye") public String hello(Model model) { model.addAttribute("message", "腾飞新星"); return "haha"; } }
http://localhost:8080/AnnotationTest/haha?myParam=nidaye
仅处理请求中包含了名为“myParam”,值为“nidaye”的请求;
3.Model和ModelAndView
在请求处理方法可出现和返回的参数类型中,最重要就是Model和ModelAndView了,对于MVC框架,控制器Controller执行业务逻辑,用于产生模型数据Model,而视图View用于渲染模型数据。
SpringMVC在内部使用了一个Model接口存储数据模型,它的功能类似java.util.Map,但是比Map容易使用。ModelMap接口实现了Map
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.game.model.User; @Controller public class User1Controller { /**ModelAttribute会优先于login调用,把请求的参数赋值给对应的变量,可以向方法中的model添加对象*/ @ModelAttribute public void userModel(String loginName,String password,Model model) { User user = new User(); user.setLoginName(loginName); user.setPassword(password); model.addAttribute("user", user); } @RequestMapping("/userlogin") public String login(Model model) { User user = (User)model.asMap().get("user"); model.addAttribute("name", user.getLoginName()); model.addAttribute("password", user.getPassword()); return "result1"; } @RequestMapping("/userlogin2") public String login(ModelMap model) { User user = (User)model.get("user"); model.addAttribute("name", user.getLoginName()); model.addAttribute("password", user.getPassword()); return "result1"; } }
ModelAttribute会优先于login调用,把请求的参数赋值给对应的变量,可以向方法中的model添加对象http://localhost:8080/AnnotationTest/userlogin1?loginName=haha&password=123
User user = (User)model.asMap().get("user");Model
User user = (User)model.get("user");ModelMap 下边看看ModelAndView
package com.game.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.game.model.User; @Controller public class User2Controller { /**ModelAttribute会优先于login调用,把请求的参数赋值给对应的变量,可以向方法中的model添加对象*/ @ModelAttribute public void userModel(String loginName,String password,ModelAndView model) { User user = new User(); user.setLoginName(loginName); user.setPassword(password); model.addObject("user", user); } @RequestMapping("/userlogin3") public ModelAndView login(ModelAndView mv) { User user = (User)mv.getModel().get("user"); mv.addObject("name", user.getLoginName()); mv.addObject("password", user.getPassword()); mv.setViewName("result1"); return mv; } }
添加模型数据用addObject
设置视图setViewName
http://localhost:8080/AnnotationTest/userlogin3?loginName=hehe&password=123