web框架之Spring-MVC环境搭建

阅读更多

spring框架jar包

1、下载spring源包

下载:spring-framework-3.1.0.RELEASE-with-docs.zip
下载依赖包:spring-framework-3.0.5.RELEASE-dependencies.zip
注意官网上3.0.3版本以后同版本依赖包不提供下载

 

2、导入所需jar包

引入dist目录下除了下面三个其余所有包
org.springframework.web.struts-3.1.0.RELEASE.jar
org.springframework.spring-library-3.1.0.RELEASE.libd
org.springframework.web.portlet-3.1.0.RELEASE.jar
引入依赖包下com.springsource.org.apache.commons.logging-1.1.1.jar及com.springsource.org.aopalliance-1.0.0.jar

 

更复杂的业务用到的jar 搭建一个简单环境可以暂不使用

org.springframework.aop-3.1.4.RELEASE.jar

org.springframework.asm-3.1.4.RELEASE.jar

org.springframework.aspects-3.1.4.RELEASE.jar

org.springframework.beans-3.1.4.RELEASE.jar

org.springframework.context-3.1.4.RELEASE.jar

org.springframework.context.support-3.1.4.RELEASE.jar

org.springframework.core-3.1.4.RELEASE.jar

org.springframework.expression-3.1.4.RELEASE.jar

org.springframework.jdbc-3.1.4.RELEASE.jar

org.springframework.orm-3.1.4.RELEASE.jar

org.springframework.transaction-3.1.4.RELEASE.jar

org.springframework.web-3.1.4.RELEASE.jar

org.springframework.web.servlet-3.1.4.RELEASE.jar

 

spring框架配置

1、web.xml配置

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">

        contextConfigLocation

       

        /WEB-INF/spring-servlet.xml

 

        org.springframework.web.context.ContextLoaderListener

 

 

spring

org.springframework.web.servlet.DispatcherServlet

1

spring

/

 

 

2、应用上下文配置
spring-servlet.xml即配置用于开启基于注解的springMVC功能,照web.xml中设定,路径为WEB-INF下

 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">

     

     

     

     

     

     

 

 

Demo例子

1、创建web项目:SpingmvcDemo

2、根据spring-servlet.xml配置的包路径(com.mvc.rest)新建RestConstroller

package com.mvc.rest;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

@Controller

public class RestConstroller {

public RestConstroller() {}

@RequestMapping(value = "/login/{user}", method = RequestMethod.GET)

public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response, 

@PathVariable("user") String user, ModelMap modelMap) throws Exception {

modelMap.put("loginUser", user);

return new ModelAndView("/login/hello", modelMap);

}

@RequestMapping(value = "/welcome", method = RequestMethod.GET)

public String registPost() {

return "/welcome";

}

 

}

 

3、建jsp视图

视图路径在spring-servlet.xml配置(/WEB-INF/view/),据上述RestConstroller 类,我们在WEB-INF下建立view目录,在view下建立welcome.jsp及login/hello.jsp
welcome.jsp随意,hello.jsp代码如下:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'hello.jsp' starting page

你好:<%=request.getAttribute("loginUser") %>,现在时间是<%= new Date() %>

 

 

 

4、部署访问
http://localhost:8080/SpringMvcDemo/welcome

 

[转] http://blog.csdn.net/linxcool/article/details/7094460 访问

你可能感兴趣的:(spingmvc)