搭建springMVC

来源:互联网 发布:linux suse 11安装yum 编辑:程序博客网 时间:2024/05/16 11:28

spring框架jar包

1、下载spring源包
spring地址:
http://www.springsource.org/download
我下的是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

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>      <!-- 配置spring核心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即配置用于开启基于注解的springMVC功能,照web.xml中设定,路径为WEB-INF下

<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="com.mvc.rest" />       <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  </beans> 

Demo例子
1、根据spring-servlet.xml配置的包路径(com.mvc.rest)新建Constroller

@Controller //命名空间@RequestMapping("/restConstroller")public class RestConstroller {    @RequestMapping(value = "/login", method = RequestMethod.GET)      public String login() {          return "index";    }  }


原创粉丝点击