springMVC环境配置

springMVC环境主要需要spring的Core Libraries和Web Libraries,使用myeclipse可以直接添加这两个包。注意:使用官方的lib时需要另外自己加入com.springsource.org.apache.commons.logging-1.1.1.jar,不然会报错。

spring-mvc.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:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
     
<!-- 激活@Required @Autowired @Resource等标签 -->
<context:annotation-config/>

<!-- DispatcherServlet上下文,指定扫描Controller标注的类 -->
<context:component-scan base-package="com.demo">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

 <!-- 默认的注解映射的支持 -->  
 <mvc:annotation-driven />  

<!-- 指定view的目录以及文件的后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/jsp/"/>
 <property name="suffix" value=".jsp"/>
</bean>

</beans>

applicationContext.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


</beans>

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_3_0.xsd" version="3.0">
  
  <display-name>springMVC</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
    <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- DispatcherServlet对应的上下文配置 -->
    <!-- 下面指定加载的 mvc-dispatcher-servlet.xml的位置-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/configs/spring-mvc.xml</param-value>
      </init-param>
     <load-on-startup>1</load-on-startup>
     </servlet>
     
     <servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <!-- 配置拦截请求的类型 -->
     <url-pattern>/</url-pattern>
     </servlet-mapping>
  
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
</web-app>

Demo.java

package com.demo;

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

@Controller
@RequestMapping("/hello")
public class Demo {
    @RequestMapping("/one")
public String hello(){
    return "hello";
}
}

 

主要是这三个配置文件的编写,然后将jsp文件放在对应的文件夹。最后访问路径是:http://服务器地址:端口/项目名/类的RequestMapping/方法的RequestMapping

posted @ 2015-08-05 09:56  ychange  阅读(257)  评论(0编辑  收藏  举报