springmvc基本配置
1.開發(fā)環(huán)境搭建,我用的是eclipse+tomcat7+jdk1.6,這部分不做介紹
2.新建工程,我新建的是dynamic web project 命名為springmvc1
3.導(dǎo)入jar包,我用的是springframework3.1.1,目前官網(wǎng)spring不能下載,我是下載的源文件,用ant編譯的。需要導(dǎo)入的jar包有:
?
4.編寫WEB-INF/web.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- springMVC 配置 --><servlet><description>spring mvc servlet</description><servlet-name>springMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><description>spring mvc 配置文件</description><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- 攔截以html為后綴的請求 --><servlet-mapping><servlet-name>springMvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><session-config><session-timeout>10</session-timeout></session-config><welcome-file-list><welcome-file>/views/index.jsp</welcome-file></welcome-file-list> </web-app>5.編寫spring-mvc.xml以及spring.xml文件
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 自動掃描controller包下的所有類,使其認(rèn)為spring mvc的控制器 --><context:annotation-config /><context:component-scan base-package="com.rainbow.controller" /><!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views" /><property name="suffix" value=".jsp" /></bean> </beans>spring.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:context="http://www.springframework.org/schema/context"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:property-placeholder location="classpath:database.properties" /><!-- 自動掃描controller,dao,service包(自動注入) --><context:annotation-config /><context:component-scan base-package="com.rainbow" /></beans>6.編寫controller文件,在src目錄下建立package
package com.rainbow.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class TestController {@RequestMapping("/hello")public String hello(){return "/hello";} }以上是簡單的java文件,
7.編寫視圖文件,hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> Hello world! </body> </html>8.run 項(xiàng)目, 我們輸入http://localhost:9090/springmvc1/hello.do便可以獲得hello world的頁面輸出,以上是spring最基本的配置。 目前并沒有顯示數(shù)據(jù)層的東西。
?
主要是給自己做一下記錄,防止忘記。
下一篇介紹,導(dǎo)入數(shù)據(jù)庫,以及hibernate。
?
?
這部分準(zhǔn)備放spring原理相關(guān)的東西
?
轉(zhuǎn)載于:https://www.cnblogs.com/snail-tomorrow/p/3482025.html
總結(jié)
以上是生活随笔為你收集整理的springmvc基本配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: #ifdef #else #endif
- 下一篇: 一张图片学Python