Use simple maven build springmvc

Introduction

Spring MVC belong SpringFrameWork subsequent products have been integrated in the Spring Web Flow inside. Spring framework provides building Web applications full-featured MVC module. Use Spring pluggable MVC architecture, so that in use Spring for WEB development time can be selected using the Spring of Spring MVC frame or integrate other MVC development framework, such as the Struts . 1 ( now generally do not ) , the Struts 2 ( typically older items used ) , etc. .

advantage

Binding for overriding the Lifecycle, Validation, etc , with easy other View frame ( Tiles seamlessly integrated, etc.), using IOC easy to test.

It is a typical textbook mvc framework, rather than the struts are all variants or are not entirely based on mvc framework of the system, for beginners or want to know mvc people who I think spring is the best, its implementation is a textbook! It is the second and tapestry as a pure servlet system, this is it and tapestry compared struts has the advantage. Frame and the code itself, seems easy to understand.

springmvc workflow

 

1.      get the browser requests go through a central controller

2.      obtained hello.do path comparing mapper pick name value hello.do

3.      find a path mapper achieved according to the obtained controller Controller Interface

4. The      data controller in response to the core controller , but the controller does not know the core ModelAndView what is

5.      The ModelAndView to view parser to parse , after parsing the core controller to give

6. The      core controller parses the view of the data after the processing

springmvc controller is a single embodiment , to avoid writing member property

Primary configuration springmvc

Introducing dependencies

<dependencies>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-web</artifactId>

                  <version>4.2.5.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-webmvc</artifactId>

                  <version>4.1.3.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-core</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-context</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-beans</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-expression</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.apache.logging.log4j</groupId>

                  <artifactId>log4j-api</artifactId>

                  <version>2.3</version>

            </dependency>

            <dependency>

                  <groupId>junit</groupId>

                  <artifactId>junit</artifactId>

                  <version>4.12</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-aop</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-test</artifactId>

                  <version>4.2.6.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-aspects</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

                  <dependency>

                  <groupId>com.alibaba</groupId>

                  <artifactId>druid</artifactId>

                  <version>1.0.9</version>

            </dependency>

            <dependency>

                  <groupId>mysql</groupId>

                  <artifactId>mysql-connector-java</artifactId>

                  <version>5.1.32</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-jdbc</artifactId>

                  <version>4.2.4.RELEASE</version>

            </dependency>

            <dependency>

                  <groupId>org.apache.tomcat</groupId>

                  <artifactId>servlet-api</artifactId>

                  <version>6.0.29</version>

            </dependency>

      </dependencies>

配置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:context="http://www.springframework.org/schema/context"

   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

   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/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-3.0.xsd">

   <bean name="/hello.do" class="com.oracle.controller.HelloController"></bean>

</beans>

映射器会自动从name属性中,获取路径

注意:没有配置核心配置文件的位置和名称的时候,应该将其和web.xml文件创建在一起,且必须命名为DispatcherServlet-servlet.xml

 

 

创建控制器

新建一个控制类实现controller方法

 

 

 

测试

将其部署到tomcat测试

 

 

更改核心配置文件的名称和位置

只需在web.xml文件中,告诉核心控制器,配置文件在哪里叫什么即可

 

 

 

编码过滤器

为每一个响应到浏览器的数据都要将其格式转换为utf-8,可以在web.xml中配置编码过滤器

 

 

但是如果拦截所有的路径,可能会将js,图片,css样式同样过滤掉,所以我们需要在springmvc.xml中配置哪些东西不需要被过滤

<mvc:resources location="/css/" mapping="/css/**"/>

      <mvc:resources location="/image/" mapping="/image/**"/>

      <mvc:resources location="/js/" mapping="/js/**"/>

配置视图解析器

我们的jsp文件如果统一放在webapp下,会显得很多很乱,因此我们可以配置视图解析器,告诉核心控制器去那里找jsp文件,找以.jsp结尾的jsp文件。

<!-- 视图解析器 -->

   <bean

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

      <property name="prefix" value="/jsps/" />

      <property name="suffix" value=".jsp"></property>

   </bean>

这样我们在自定义的控制器中,就可以只写jsp文件的名字即可。

 

 

Guess you like

Origin www.cnblogs.com/mhm111/p/11422405.html