基于注解的Spring MVC控制器
来源:互联网 发布:玉米田间调查数据 编辑:程序博客网 时间:2024/06/05 16:07
1.在web.xml中配置前端控制器的DispatcherServlet
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
- <display-name>AnnotationTest</display-name>
-
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/springmvc-config.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
2.修改Spring MVC的配置文件(springmvc-config.xml)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd">
-
-
- <context:component-scan base-package="org.fkit.controller"/>
-
-
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
-
-
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
-
- </beans>
3.基于注解类型实现Controller类,用来处理/hello请求(HelloController.java)
- package org.fkit.controller;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
-
-
-
-
-
- @Controller
- public class HelloController {
- private static final Log logger=LogFactory.getLog(HelloController.class);
-
-
-
-
-
-
-
-
- @RequestMapping(value="/hello")
- public ModelAndView hello(){
- logger.info("hello方法 被调用");
-
- ModelAndView mv = new ModelAndView();
-
- mv.addObject("message", "Hello World!");
-
- mv.setViewName("/WEB-INF/content/welcome.jsp");
-
- return mv;
- }
-
- }
4.视图界面(welcome.jsp)
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>welcome</title>
- </head>
- <body>
-
- ${requestScope.message}
- </body>
- </html>
5.截图
0 0