标签:padding 相关 nts top ble base strong request system
创建一个web工程,添加spring所有相关的包。以下用两种方式来搭建第一个springmvc, 非注解及注解的方式
1)、非注解的方式
a)在web.xml配置文件中配置前端总控制器
<!-- 前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <!-- 1).action 2)/*: 3)/: --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> |
b)在spring-mvc.xml文件中配置:
<!-- 前端处理映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean name="/query.action" class="cn.hd.controller.HelloController"></bean>
<!-- 前端处理适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
c)控制层实现controller接口
public class UserController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv=new ModelAndView(); mv.addObject("str", "我是测试的"); mv.addObject("msg", "再来测试一次"); mv.setViewName("/index.jsp");
return mv; } } |
2)注解的方式:
1)在springmvc配置文件中
<!-- 开启注解方式: -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="cn.hd.controller"></context:component-scan>
2)在controller中的类:
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/query")
public ModelAndView query() {
System.out.println("这是注解的方法:helloworld。。。。");
ModelAndView mv = new ModelAndView();
mv.addObject("str", "这是ModelAndView中model的值");// 等价于request.setAtrribute("str",
// "这是ModelAndView中model的值")
mv.setViewName("/index.jsp");// 要跳转的页面
return mv;
}
标签:padding 相关 nts top ble base strong request system
原文地址:http://www.cnblogs.com/sunachclp/p/6394714.html