SpringMVC的搭建

来源:互联网 发布:b2b软件有哪些 编辑:程序博客网 时间:2024/06/14 17:31

使用eclipse通过maven建立SpringMVC
1、建立一个maven工程
2、webapp下建立一个WEB-INF文件夹,并添加web.xml(模板在最后,标注为1)
3、在pom.xml中添加两个包:Spring Context和Spring Web Mvc
这一步之后,正常情况下会出现8个依赖的包

<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>4.3.6.RELEASE</version>    </dependency>    <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-webmvc</artifactId>    <version>4.3.6.RELEASE</version>    </dependency>

4、配置Controller

@Controllerpublic class StudentController {    @RequestMapping("/index")    public String getStudent() {        return "index";    }}

解释:

@Controller 此注释说明此类是一个Controller@RequestMapping("/index")   此注释的意思是访问到index,那么运行下面的方法return "index"; 此返回值返回一个网页的前缀,比如此方法针对的就是index.jsp

5、写要访问的网页

6、如何配置地址
由于SpringMVC已经把代码写好,所以不能改变源代码,只能通过web.xml配置

<servlet>    <servlet-name>MVC</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping>    <servlet-name>MVC</servlet-name>    <url-pattern>/</url-pattern></servlet-mapping>

解释:

    1<servlet-name>自己起的名字    2<servlet-class>spring-webmvc下的org包下的DispatcherServlet的全路径去掉.class    3<url-pattern>要操作的地址

此时,如果启动,那么不报错,但是如果访问,就报错,报错原因是缺少webmvc.xml文件,报错截图如下:
这里写图片描述

7、添加 名字-servlet.xml文件WEB-INF目录下(模板在最后,标注为2)

8、配置servlet.xml文件

<!-- 开启注解驱动 -->    <mvc:annotation-driven></mvc:annotation-driven><!-- 配置controller所在的包 -->    <context:component-scan base-package="Controller"></context:component-scan><!-- 配置视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>        <property name="prefix" value="/WEB-INF/pages/"></property>        <property name="suffix" value=".jsp"></property>    </bean>

解释:

1、<mvc:annotation-driven>固定的2、<context:component-scan base-package="路径">路径为controller类所在的包的名字,在controller.class父级包上,获取到需要的全路径3、<bean class="路径">路径为spring-webmvc下view文件夹下InternalResourceViewResolver类获取全路径然后去掉.class后缀4、<property name="viewClass" value="路径">此路径为spring-webmvc下view文件夹JstlView的全路径去掉.class5、<property name="prefix" value="路径">文件前缀,在方法的返回值前面加的东西,路径为网页存放路径6、<property name="suffix" value=".jsp">文件后缀,在方法的返回值后面添加的东西

9、添加jstl包

<dependency>    <groupId>javax.servlet</groupId>    <artifactId>jstl</artifactId>    <version>1.2</version>    </dependency>

10、完成,这就是SpringMVC的搭建。


备注1(web.xml模板):

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"></web-app>

备注2(MVC-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:util="http://www.springframework.org/schema/util"     xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd       http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util-4.0.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">    <!-- 开启注解驱动 -->    <mvc:annotation-driven></mvc:annotation-driven>    <!-- 配置controller所在的包 -->    <context:component-scan base-package="Controller"></context:component-scan>    <!-- 配置视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>        <property name="prefix" value="/WEB-INF/pages/"></property>        <property name="suffix" value=".jsp"></property>    </bean></beans>
原创粉丝点击