SpringMVC坏境搭建
---------------------------qhz
配置坏境:
Windows 8.1
JDK 1.7(1.8我试的时候出问题了 果断换7)
Tomcat 6.0 或者Tomcat 8.0
1. 新建动态网页
2. 导入相应的jar包
最终目录结构为
3. 配置Web.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//SunMicrosystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
<display-name>SpringMvcDemo</display-name>
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. 配置 name-servlet.xml(这里的name 需要 与web.xml 中的name 对应)
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- 静态资源(js、image等)的访问 -->
<mvc:default-servlet-handler/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 配置映射处理器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- name为请求URL class为请求处理器 -->
<bean name="/hello.do"class="cn.qhz.controller.HelloController"></bean>
<!-- 配置处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 配置视图解析器 -->
<bean id="ViewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
<!-- 视图解析器的前缀将返回的ModelView名前加上前缀 -->
<property name="prefix"value="/WEB-INF/jsp/"/>
<!-- 视图解析器后缀 如果控制器返回页面返回名 hello 最终结果为/WEB-INF/jsp/hello.jsp -->
<property name="suffix"value=".jsp"/>
</bean>
</beans>
这里的话需要导入jstl的jar包
5. 新建包 (控制器)
packagecn.qhz.controller;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.web.servlet.ModelAndView;
importorg.springframework.web.servlet.mvc.Controller;
/**
* 注意导入包 为org.springframework.web.servlet.mvc.Controller
* @author me
*实现mv的处理器
*/
public classHelloController implements Controller {
@Override
public ModelAndViewhandleRequest(HttpServletRequest req,
HttpServletResponse resp)throws Exception {
ModelAndView mv=newModelAndView();
//设置回显结果
/*req.setAttribute("msg","helloSpringmvc");
req.getRequestDispatcher("hello.jsp").forward(req,resp);*/
mv.addObject("msg","helloSpringmvc");
mv.setViewName("hello");
return mv;
}
}
7.View页面
<%@ page language="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>SpringMVc</title>
</head>
<body>
${msg }
</body>
</html>
8.将项目部署到Tomcat 上 开启服务
9.查看日志 是否报错