1、下載 spring 包,http://www.springsource.org/download
我下的是spring-framework-4.0.6.RELEASE
2、新建 web項目,在/WebRoot/lib 導入.jar包(/spring-framework-4.0.6.RELEASE/libs目錄下)
spring框架配置
1、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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 應用上下文配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置servlet 核心servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern配置為/,不帶文件后綴,會造成其它靜態文件(js、CSS等)不能訪問。 如配置為*.do,則不影響靜態文件的訪問 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2、應用上下文配置文件,spring-servlet.xml(web中定義存放路徑為:/WEB-INF/spring-servlet.xml)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 啟動注解驅動的Spring MVC功能,注冊請求url和注解 POJO 類方法的映射 --> <mvc:annotation-driven/> <!-- 啟動包掃描功能,以便注冊帶有@Controller、@Service、@repository、@Component等 注解的類成為spring的bean --> <context:component-scan base-package="sendi.znwg.rest"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view" p:suffix=".jsp"></bean> </beans>
3、Demo例子
根據spring-servlet配置的包路徑(sendi.znwg.rest),新建RestController.java
@Controllerpublic class RestController { public RestController(){ } @RequestMapping(value = "/login/{user}",method=RequestMethod.GET) public ModelAndView rest(HttpServletRequest request, HttpServletResponse response,@PathVariable("user")String user,ModelMap modelMap){ modelMap.put("loginUser", user); return new ModelAndView("/login/hello",modelMap); } @RequestMapping(value="/welcome",method = RequestMethod.GET) public String registPost(){ return "/welcome"; } }
可以自定義jsp(視圖路徑/WEB-INF/view 下)
hello.jsp
<%@page import="java.util.Date"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!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=utf-8"><title>hello 頁面</title></head><body><h2> 你好:<%=request.getAttribute("loginUser") %>,現在的時間是<%=new Date() %></h2></body></html>
tomcat部署后訪問:
http://localhost:8080/spring-mvcDay2/login/gzsendi
新聞熱點
疑難解答