1.导入jar包
commons-logging-1.1.1.jar jackson-annotations-2.5.4.jar jackson-core-2.5.4.jar jackson-databind-2.5.4.jar spring-aop-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar spring-expression-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar spring-web-4.1.6.RELEASE.jar spring-webmvc-4.1.6.RELEASE.jar
2.配置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:mvc.xml</param-value> </init-param> <!--设置这个servlet和服务器同步启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3.创建配置文件mvc.xml在目录src下
4.创建包com.ahd.controller,在包内创建类HelloController
package com.ahd.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("hello") public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp){ ModelAndView mv=new ModelAndView(); mv.addObject("msg", "controller annotat8ion"); mv.setViewName("hello"); return mv; } }
5.修改配置文件mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置渲染器 --> <bean id="jspViewResolver" 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"/> <!-- 例:视图名为:hello 渲染后:/WEB-INF/jsp/hello.jsp 该页面--> </bean> <!-- 扫描指定包内的注解 --> <context:component-scan base-package="com.ahd.controller"></context:component-scan> </beans>
6.在web-INF目录中建立文件夹jsp,在文件夹中建立文件hello.jsp,和视图渲染器配置相一致
添加代码
${msg}