自己搭建springmvc的步骤和一些问题的总结

来源:互联网 发布:java 实现在线订票系统 编辑:程序博客网 时间:2024/05/17 07:31

第一步:


pom文件导入springmvc所依赖的jar  如下:


  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>3.0.7.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>3.0.7.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>3.0.7.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.7.1</version>
  </dependency>



第二步:

 配置web.xml  如下:

 <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 可以自定义servlet.xml配置文件的位置和名称,不配置init-param也可以默认为WEB-INF目录下,名称为 -->
  <!--[<servlet-name>]-servlet.xml,如spring-servlet.xml 名称的后缀必须是-servlet ,前面随意
   和 servlet-name名称一致就行 -->
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:com/pzx/springConfig/*.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>


第三步:

配置springMVC-servlet.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"    
        xmlns:context="http://www.springframework.org/schema/context"   
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
   xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
       http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd  
       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">
      
      
    <!-- 启用spring mvc 注解 -->       
 <mvc:annotation-driven></mvc:annotation-driven> 
 <!-- 设置使用注解的类所在的jar包 --> 
    <context:component-scan base-package="com.pzx.*"></context:component-scan>
   
   
     <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀   就是你存放的路劲,不然会爆404找不到jsp-->   
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"></property> 
     <property name="suffix" value=".jsp"></property> 
 </bean> 
      
    </beans>


第四步  :


编写helloword请求处理类


package com.pzx.controller;

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

@Controller
@RequestMapping("/helloWorldController")
public class HelloWorldController {

 @RequestMapping("/sayHello")
 public String sayHello() {
  System.out.println("HelloWorldController!!!!!!!!!!!!!!!!!!!!!!");

  return "helloWorld";

 }
 @RequestMapping("/sayHello2")
 public ModelAndView sayHello2() {
  System.out.println("HelloWorldController2!!!!!!!!!!!!!!!!!!!!!!");
  
  return new ModelAndView("helloWorld");
  
 }
}

这两种接受请求并返回 写法效果一样  都是返回helloWorld.jsp


以上配置完后 启动tomcat  浏览器输入:http://localhost:8088/TestMavenAndSpringMvc/helloWorldController/sayHello


在配置过程也遇到了一些问题  现 整理如下;

1:元素 "mvc:annotation-driven" 的前缀 "mvc"未绑定

解决:这是我在spring-servlet.xml文件里使用<mvc>开头的标签时,忘记引入了命名空间。在xml的beans里面加入如下代码即可

  • xmlns:mvc="http://www.springframework.org/schema/mvc"  
  •   
  • http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 


    2:  No mapping found for HTTP request with URI


    原因spring没有扫描到工程的包 导致找不到请求

    解决:    <context:component-scan base-package="com.pzx.*"></context:component-scan>   这个配置要仔细看下 是否匹配



    以上是自己的整理的 于大家分享  后续 我会更新一篇springmvc 整合hibernate  和spingmvc整合mybatis的文章 敬请持续关注!!

     











    
  • 1 0
    原创粉丝点击