masterJ2EE篇002——springmvc简单实例
2016-11-05 14:31
295 查看
1、新建java dynamic web project,将springmvc3.0所需jar包放到WEB-INF/lib下。
2、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>masterSpringMvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
3、springmvc配置文件springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.zls.master.springmvc.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 -->
<value>/WEB-INF/vm/</value>
</property>
<property name="suffix">
<value>.vm</value>
</property>
</bean>
</beans>
4、controller
package com.zls.master.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping("/home.htm")
public ModelAndView home(){
return new ModelAndView("home");
}
}
5、index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
this is index page.
</body>
</html>
6、home.htm只有一句话
this is home page.
7、访问http://localhost:8080/masterSpringMvc/index.html
由于这个请求没有被dispatcherServlet(springmvc)拦截,又在WEB-INF外,所以直接返回了。
8、访问http://localhost:8080/masterSpringMvc/home.htm
9、项目结构图
2、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>masterSpringMvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
3、springmvc配置文件springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.zls.master.springmvc.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 -->
<value>/WEB-INF/vm/</value>
</property>
<property name="suffix">
<value>.vm</value>
</property>
</bean>
</beans>
4、controller
package com.zls.master.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping("/home.htm")
public ModelAndView home(){
return new ModelAndView("home");
}
}
5、index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
this is index page.
</body>
</html>
6、home.htm只有一句话
this is home page.
7、访问http://localhost:8080/masterSpringMvc/index.html
由于这个请求没有被dispatcherServlet(springmvc)拦截,又在WEB-INF外,所以直接返回了。
8、访问http://localhost:8080/masterSpringMvc/home.htm
9、项目结构图
相关文章推荐
- Maven学习系列(二) 学会看mvnrepository及springmvc简单实例
- SpringMVC配置,简单实例,文件上传与下载,ajax请求
- SpringMVC 基础教程 简单入门实例
- SpringMVC笔记——SSM框架搭建简单实例
- SpringMVC笔记——Spring+MyBatis组合开发简单实例
- SpringMVC-(1)一个简单的实例demo及讲解
- 简单的SpringMvc实例
- springMVC 简单实例
- Java技术_每天掌握一种设计模式(002)_使用场景及简单实例(创建型:单例模式)
- springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目
- spring MVC 入门(实例一)创建简单springMVC
- SpringMVC 简单实例
- springmvc整合hibernate简单实例
- SpringMVC 简单实例
- 最简单的springMVC实例
- SpringMVC 基础教程 简单入门实例
- HTML 简单教程-实例002 HTML标题<h1> - <h6> 标签
- SpringMVC 理论与实用技术(一) 简单、实用、易懂的几个实例
- 一个简单的springMVC的定时任务实例
- SpringMVC 理论与有用技术(一) 简单、有用、易懂的几个实例