Spring框架

来源:互联网 发布:netbeans开发php 编辑:程序博客网 时间:2024/05/29 18:51


    1. 创建springmvc需要引用到的jar包

         

   2   web.xml配置


     <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>spring</display-name>   
   
    <servlet> 
        <servlet-name>spring</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
        </servlet> 
    <servlet-mapping> 
        <servlet-name>spring</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
     
    <welcome-file-list>
        <welcome-file>hello.jsp</welcome-file>
    </welcome-file-list>
</web-app>

     3.  spirng-servlet配置


  <?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"    
    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.0.xsd     
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context-3.0.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
     
    <bean name="/hello" class="com.bj58.controller.HelloController" /> 
     
    <!-- ViewResolver --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views"/> 
        <property name="suffix" value=".html"/> 
    </bean> 
</beans>
补充:
 <!-- 注解映射的支持   --><mvc:annotation-driven/> <!-- 使用默认的Servlet来响应静态文件 --> <mvc:default-servlet-handler/> <!--需要配一个自动扫描包的,不然无法映射--> <context:component-scan base-package="com.controller" /> <!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

  4.controller


package com.bj58.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


public class HelloController implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0,
           HttpServletResponse arg1) throws Exception {
       System.out.println("--------------hello world! 世界你好!--------------------");
       return new ModelAndView("/hello");
   }
}

5.启动服务器

http://localhost:8080/TomcatTest/hello

http://localhost:8080/项目名/hello

6.目录结构



重要tomcat的配置
http://jingyan.baidu.com/article/ca2d939dd90183eb6d31ce79.html



                


0 0
原创粉丝点击