您的位置:首页 > 编程语言 > Java开发

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

2016-08-30 15:41 405 查看
第一步:

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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
       http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd  
       http://www.springframework.org/schema/context
http://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的文章 敬请持续关注!!

 


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc