天天看点

spring mvc 环境搭建 demo 的开发

  exclipse 把spring mvc 搭建好了之后需要做什么。

 1 首先 调整到一个默认的页面,启动之后验证是否成功

      在web.xml 加上如下代码,即可看到

<welcome-file-list>
        <welcome-file>index.html</welcome-file>
     </welcome-file-list>      
2 写一个democontroller ,使 通过controller层,到达jsp 页面。需要配置如下      
web.xml 添加如下信息      
<!--加载context.xml 文件 -->      
<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
            classpath:/context/spring-context.xml  
        </param-value>  
    </context-param>        
<!-- 加载 .xml 文件-->       
<listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      
<servlet>
         <servlet-name>study</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
<init-param>      
<param-name>contextConfigLocation</param-name>         
                    <param-value>   classpath:/context/spring-context.xml </param-value>       
</init-param>      
<load-on-startup>1</load-on-startup>      
</servlet> <servlet-mapping> <servlet-name>study</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
在 src/main/resources 下面建 context 文件夹        
在context 文件夹 新建 spring-context.xml 文件      
在该文件夹下面 新建 servlet 文件夹      
在servlet 文件夹 新建 servlet.xml ,内容如下      
<!-- 激活@Controller模式 -->
     <mvc:annotation-driven />
    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
     <context:component-scan base-package="web.controller" />
      
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix">      
<value>/WEB-INF/jsp/</value>
        </property>
         <property name="suffix">
            <value>.jsp</value>
         </property>
     </bean>      
新建DemoController       
@Controller       
public class DemoController {      
@requestMapping(value="/demo/study",method = RequestMethod.GET)      
public string demoTest(ModelAttribute("cmd") DemoCommand cmd){      
return "/demo/demo";      
}      
}      
3    一个应用必须要打印日志, 因此在web.xml 新加日志文件      
<context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <param-value>  
            classpath:/context/log/log4j.xml
        </param-value>  
    </context-param>        
4 应用中需要用到的 context.properties 文件,配置的信息 因此,需要加载此信息      
在 spring-context.xml 文件中加 如下信息      
<context:property-placeholder location="classpath:/context/properties/context.properties"  />      

根据以上内容的学习,发现 ContextLoaderListener 和 DispatcherServlet 加载的xml文件的区别,具体见http://blog.csdn.net/liweiahut/article/details/49735023