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

J2ee项目从0搭建(六):Spring MVC集成

2016-06-05 20:13 615 查看

一、pom.xml中配置依赖jar:

<span style="white-space:pre">		</span><!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>


二、web.xml:需要配置<servlet>及<servlet-mapping>来对请求进行拦截,具体参数如下:

<!-- spring mvc中实际起到拦截、分发、路程控制的是 DispatcherServlet类-->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化spring mvc 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-mvc.xml</param-value>
</init-param>
<!-- 标示启动容器时初始化Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 表示对应的哪些请求交给spring mvc处理 -->
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<!-- "/"表示默认所有的请求都通过servlet映射,如"*.html"表示拦截所有以html为扩展名的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>


并且,我们需要将<web-app>标签增加命名空间和servlet版本属性,如果不加的话,最直观的问题就是我们再后台想前台传值时,无法使用EL表达式${value}去接收参数:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" version="3.0">


三、spring-mvc.xml:在二中我们使用的spring-mvc.xml还没有创建,classpath代表类路径下,就是我们前面讲到的创建的文件夹,并定义为源文件:src/main/resources文件。



在这里我们通过<context:component-scan>配置一个mvc分发自动扫描的入口。
再配置一个视图解析器InternalResourceViewResolver,用来进行页面的展示,这里我们配置的是所有WEB-INF下的以jsp为后缀的文件。
另外,在<beans>跟节点中我们需要加入命名空间。

<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
<!-- 自动扫描com.spring.demo.mvc -->
<context:component-scan base-package="com.spring.demo.mvc" />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />

</bean>

</beans>


四、HelloController:我们写了一个Controller,并向前台的hello页面传递一个参数name,URL中的路径与@RequestMapping映射的值相对应,因为上一步配置的后缀是“.jsp”后缀,所以这边返回时可以不写后缀名,如下使用:

package com.spring.demo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/mvc")
public class HelloController {

@RequestMapping("/hello")
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
Model model) {
model.addAttribute("name", name);
return "/demo/hello";
}

}


五、hello.jsp:在页面上使用EL表达式去接收后台传递过来的参数:

package com.spring.demo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/mvc")
public class HelloController {

@RequestMapping("/hello")
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
Model model) {
model.addAttribute("name", name);
return "/demo/hello";
}

}


六:效果:URL:http://localhost:8080/buildmavenweb/mvc/hello?name=springmvc



七、为了便于大家学习,项目被开源在我的github上:

项目地址:https://github.com/gubaijin/buildmavenweb



如何将项目开源道github,请看我的另一篇博文:GitHub上创建项目 并初始化本地工程提交到GitHub上

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