Spring MVC 入门示例http://cuisuqiang.iteye.com/blog/2042931中,配置Controller时使用的是URL对应Bean的方式
在SpringMVC中,对于Controller的配置方式有很多种,如下做简单总结
第一种 URL对应Bean
如果要使用此类配置方式,需要在XML中做如下样式配置
<!-- 表示将请求的URL和Bean名字映射--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean name="/hello.do" class="test.HelloController"></bean>
以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅适用小型的应用系统
第二种 为URL分配Bean
使用一个统一配置集合,对各个URL对应的Controller做关系映射
<!-- 最常用的映射配置方式 --> <!-- <prop key="/hello*.do">helloController</prop>--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.do">helloController</prop> </props> </property> </bean> <bean name="helloController" class="test.HelloController"></bean>
此类配置还可以使用通配符,访问/hello.do时,Spring会把请求分配给helloController进行处理
第三种 URL匹配Bean
如果定义的Controller名称规范,也可以使用如下配置
<!-- 将hello*.do交给helloController处理--> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean name="helloController" class="test.HelloController"></bean>
第四种 注解
首先在配置文件中开启注解
<!-- 启用 spring 注解 --> <context:component-scan base-package="test" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
在编写类上使用注解@org.springframework.stereotype.Controller标记这是个Controller对象
使用@RequestMapping("/hello.do")指定方法对应处理的路径,这里只是简单示例,会有更复杂配置
代码类如下:
package test; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; // http://localhost:8080/spring/hello.do?user=java @org.springframework.stereotype.Controller public class HelloController{ @SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response){ request.setAttribute("user", request.getParameter("user") + "-->" + new Date().toLocaleString()); return "hello"; } }
附件是源码,导入Eclipse查看示例。
请您到ITEYE网站看 java小强 原创,谢谢!
http://cuisuqiang.iteye.com/ !
自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!
相关推荐
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
spring mvc controller间跳转 重定向 传参
基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...
9 import org.springframework.web.servlet.mvc.Controller; 10 import org.springframework.web.servlet.ModelAndView; 11 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet....
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...
The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also ...
学习Spring MVC,关于注解的Spring MVC,简单例子,关于控制器的Spring MVC,学习Spring,简单Spring MVC实例
Spring mvc依赖包 Spring mvc都做了些什么 Controller为中心完成对系统流程的控制管理 从请求中搜集数据 对传入的参数进行验证 将结果返回给视图 针对不同的视图提供不同的解决方案 针对jsp视图技术提供标签库 拦截...
uploadify的demo,解决spring MVC上传配置冲突。保留springmvc的配置,修改controller中的代码。
Spring MVC之Controller层的常用注解
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。, ...
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界主流的Web开发框架,Spring MVC已经成为当前热门的开发技能,同时也广泛用于桌面开发领域。 ...
Spring MVC is a modern web application framework built upon the Spring Framework, and Spring Web Flow is a project that complements Spring MVC for building reusable web controller modules that ...
Spring MVC基于Model-View-Controller(MVC)架构模式,优化了Web应用程序的设计和开发。在Spring MVC中,DispatcherServlet作为前端控制器,负责请求的接收和响应结果的处理。处理器映射器(HandlerMapping)和...
The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also ...
Spring mvc注解方式实现Controller