标签:日志 handle 路径 环境搭建 common nts page servlet 技术
M1:环境搭建:spring依赖jar包,web.xml配置,spingmvc-config.xml配置
M2:新建Dynamic Web Project工程springMVC,记得勾选上web.xml。
M3:下载spring4.0工程文件,打开libs将里面的jar包复制到springMVC的lib下。
spring4.0工程文件可以到官网下载,这里提供csdn分享的链接:
http://download.csdn.net/detail/qqdutu/7328329
云盘连接:http://pan.baidu.com/s/1pKF8w8V 密码:5yvu
导入效果如图:
M4:配置web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns="http://java.sun.com/xml/ns/javaee" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 7 id="WebApp_ID" version="3.0"> 8 <display-name>springMVC</display-name> 9 10 <!-- 配置前端控制器 --> 11 <servlet> 12 <!-- servlet名称 --> 13 <servlet-name>springmvc</servlet-name> 14 <!-- servlet对应java类 --> 15 <servlet-class > 16 org.springframework.web.servlet.DispatcherServlet 17 </servlet-class> 18 <!-- servlet参数设置 --> 19 <init-param> 20 <!-- 参数设置名称 --> 21 <param-name>contextConfigLocation</param-name> 22 <!-- 该参数包含的配置文件路径 --> 23 <param-value>/WEB-INF/resource/springmvc-config.xml</param-value> 24 </init-param> 25 <!-- web应用启动时加载Servlet --> 26 <load-on-startup>1</load-on-startup> 27 </servlet> 28 <!-- servlet映射声明 --> 29 <servlet-mapping> 30 <!-- 请求servlet的名称 --> 31 <servlet-name>springmvc</servlet-name> 32 <!-- 监听当前域的所有请求--> 33 <url-pattern>/</url-pattern> 34 </servlet-mapping> 35 </web-app>
M5:配置springmvc-config.xml
在WEB-INF 下面新建文件夹 resource再在resource里面新建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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置Handle,映射"/hello请求" --> <bean name="/hello" class="com.mollen.controller.HelloController"/> <!-- 处理映射器将bean的name作为url进行查找,配置Handle指定name(url) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- SimpleControllerHandlerAdapter是一个处理适配器,所有处理适配器都要实现HandleAdapter接口 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>
M6:编写class测试类HelloCotroller
1 package com.mollen.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.servlet.ModelAndView; 10 import org.springframework.web.servlet.mvc.Controller; 11 12 public class HelloController implements Controller{ 13 14 private static final Log log = LogFactory.getLog(HelloController.class); 15 16 @Override 17 @ResponseBody 18 public ModelAndView handleRequest(HttpServletRequest Request, HttpServletResponse Response) 19 throws Exception { 20 //打印日志 21 log.info("handleResquest 被调用"); 22 //创建准备返回的modelandview对象(返回视图名,模型名称,模型对象) 23 ModelAndView mv = new ModelAndView(); 24 //添加模型数据,可以是任意pojo 25 mv.addObject("message", "Hello World"); 26 //设置逻辑视图名 27 mv.setViewName("/WEB-INF/content/welcome.jsp"); 28 //返回ModelAndView对象 29 return mv; 30 } 31 32 }
M7:页面welcome.jsp编辑
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 4 "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>welcome</title> 9 </head> 10 <body> 11 ${requestScope.message} 12 </body> 13 </html>
M8:部署访问
标签:日志 handle 路径 环境搭建 common nts page servlet 技术
原文地址:http://www.cnblogs.com/mollenblogs/p/7290021.html