Spring MVC 环境搭建(二)

时间:2023-03-09 03:14:30
Spring MVC 环境搭建(二)

Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面。

但这存在一个问题,当我们项目的页面很多时,这样一个映射对应一个控制器,配置文件将会很臃肿!

其实在新版本的spring中,urlMapping 已经基本不用,而是采用注解机制。

spirng 注解

注解实际上相当于一种标记,它允许你在运行时动态地对拥有该标记的成员进行操作。

实现注解需要三个条件:注解声明、使用注解的元素、操作使用注解元素的代码。

一、注解配置

1、首先在 spring-servlet.xml 配置文件中,修改 context 命名空间的申明。
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
2、在 spring-servlet.xml 注释掉 urlMapping 中添加:
     <!-- 通过该语句可以扫描com.myweb及com.myweb下子包中的类 -->
<context:component-scan base-package="com.myweb"></context:component-scan> <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>
<!--<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.html">IndexAction</prop>
</props>
</property>
</bean> -->

如下图所示:

Spring MVC 环境搭建(二)

二、相关注解说明

1、@Controller

当我们使用注解后,控制器可以不再去实现 Controller 接口,只需在类的头部加上 @Controller,告诉 spring 该类就是控制器,spring 则会帮你自动装配。

2、@RequestMapping

Spring 通过 RequestMapping 来指定访问控制器方法来转向页面。 在老版本的 Spring 中,如果不同的 url 访问同一个控制器,要使用多动作控制器(MultiActionController )。
使用:在控制器中的方法前加上 RequestMapping 

三、注解使用

1、假如我们访问 http://localhost:8080/MyWeb/news 就显示新闻页面

 package com.myweb;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class NewsAction { @RequestMapping("/news")
public ModelAndView ShowNews() { ModelAndView mv = new ModelAndView("news"); // 默认为 news
mv.addObject("content", "这是新闻页面");
return mv;
} }

1.2、修改 web.xml 的 url 拦截配置

 <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

2、假如我们访问 http://localhost:8080/MyWeb/news?id=1 就显示新闻 id=1 的列表页面

 package com.myweb;

 import com.myweb.tool.BarFactory;
import com.myweb.tool.NavBar;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
public class NewsAction { @RequestMapping("/news")
public ModelAndView ShowNewsDetail(@RequestParam(value = "id", required = false) String id) { // news?id=1 则触发此方法
ModelAndView mv = new ModelAndView("news"); // 默认为 news NavBar topBar = BarFactory.createBar("top");
NavBar bottomBar = BarFactory.createBar("tottom");
mv.addObject("top_nav", topBar.getBarContent());
mv.addObject("bottom_nav", bottomBar.getBarContent()); if (id == null) {
mv.addObject("content", "这是新闻列表页面, 没有 ID 参数");
} else {
mv.addObject("content", "这是新闻页面, id 是: " + id);
} return mv;
} }

3、假如我们访问 http://localhost:8080/MyWeb/news/1/123456 就显示管理新闻 id=1 的列表页面

 package com.myweb;

 import com.myweb.tool.BarFactory;
import com.myweb.tool.NavBar;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class NewsAction { @RequestMapping("/news/{id}/123456")
public ModelAndView ShowNewsDetail2(@PathVariable String id) { // news/1/123456 则触发此方法
ModelAndView mv = new ModelAndView("news"); // 默认为 news NavBar topBar = BarFactory.createBar("top");
NavBar bottomBar = BarFactory.createBar("tottom");
mv.addObject("top_nav", topBar.getBarContent());
mv.addObject("bottom_nav", bottomBar.getBarContent()); mv.addObject("content", "这是新闻列内容页面(用PathVariable的方式), id 是:" + id); return mv;
} }