springmvc初步配置

  1. 导包/添加依赖:

<dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-aop</artifactId>

            <version>4.0.2.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-beans</artifactId>

            <version>4.0.2.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>4.2.2.RELEASE</version>

            <type>pom.lastUpdated</type>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>4.0.2.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-tx</artifactId>

            <version>4.0.2.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>4.0.2.RELEASE</version>

        </dependency>

       

2.web.xml文件配置

 

<!-- springmvc  -->

    <servlet>

        <servlet-name>springMVC</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet

</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-mvc.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>

3.上面配置里面classpath:spring-mvc.xml,所以去创建一个spring-mvc.xml文件

注意位置是直属resources

里面配置

   <!-- 启用spring mvc 注解 -->

    <context:annotation-config/>

    <!-- 配置扫描controller包 -->

    <context:component-scan base-package="test.tt.controller.*" />

    <!-- 完成请求和注解POJO的映射 -->

    <bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver"

        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

4.根据位置创建包和类,来自这里

 

<context:component-scan base-package="test.tt.controller.*" />

5.创建返回的页面

 

页面位置来自:p:prefix="/WEB-INF/views/"

页面类型/后缀:p:suffix=".jsp"

页面名称:return new ModelAndView("test")这个返回的字符串;

6.修改欢迎界面,

7.启动测试

8.乱码解决:在index界面加上,这里是网页读取信息,没走拦截器,web.xml不管用

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

9.点击超链

 

ps:如果出现一个no fondPage 和no mapping的错误,就把web.xml的协议头换下

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"></web-app> 

posted @ 2016-12-12 18:29  天涯已可  阅读(319)  评论(0编辑  收藏  举报