Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面。
但这存在一个问题,当我们项目的页面很多时,这样一个映射对应一个控制器,配置文件将会很臃肿!
其实在新版本的spring中,urlMapping 已经基本不用,而是采用注解机制。
spirng 注解
注解实际上相当于一种标记,它允许你在运行时动态地对拥有该标记的成员进行操作。
一、注解配置
<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">
<!-- 通过该语句可以扫描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> -->
如下图所示:

二、相关注解说明
1、@Controller
2、@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;
} }
Spring MVC 环境搭建(二)的更多相关文章
- Spring MVC 环境搭建(一)
一.建立 JavaWeb 项目 1.建立一个 Java 项目. 2.在项目下新建一个文件夹 webapp (命名可自取,这个目录即是网站根目录),再在该文件夹下新建一个 WEB-INF 文件夹(命名固 ...
- Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)
Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...
- Java学习笔记之:Spring MVC 环境搭建
一.创建项目 1.新建java动态项目 2.在web-inf/lib文件夹下导入jar 3.创建所需要的包和文件 二.搭建SpringMVC 1.配置web.xml(WEB-INF下) <?xm ...
- spring MVC 环境搭建
绿色版Spring MVC(单纯的springMVC) 一.导包,为了获取请求数据多添加一个包 二.web.xml配置 <?xml version="1.0" encodin ...
- [Spring MVC] - Spring MVC环境搭建
1) 复制Spring所需要的lib包 (这是SSH所需要的lib包,如果只使用spring,可以移除一些包) 2) 配置web.xml <?xml version=" ...
- spring MVC环境搭建
1.新建web项目,并在web.xml加入spring mvc的servlet <!-- spring mvc容器和servlet的定义 --> <servlet> <s ...
- Spring MVC环境搭建和配置
1. 创建Dynamic web project 2. 修改WEB-INF/web.xml,内容如下: <?xml version="1.0" encoding=" ...
- Spring MVC: 环境搭建并实现简易的HelloWorld
第一步:使用配置Tomcat服务器的Eclipse新建一个名为“TestSpringMVC”的web项目 第二步:将所使用的jar包复制到WEB-INF/lib目录下 第三步:在web.xml中配置D ...
- spring mvc 框架搭建及详解
现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...
随机推荐
- 为什么for in循环不适合用于数组
首先一点无关的,使用(var i in a) 而不是( i in a),除非你想创建全局变量. 第二点,for in 循环会忽略空的数组 var a = []; a[5] = 5; // Perfec ...
- Winform主窗体的设置
软件必然涉及到一个主窗体MainForm,下面介绍一下几个简单的属性设置,可能比较有用 (1)icon,当然是咱们软件的图标了,设置上去即可 (2)isMdiContainer,这个比较重要了哦,必须 ...
- SQL中补0
SQL中补0 编写人:CC阿爸 2014-3-14 第一种方法: right('00000'+cast(@count as varchar),5) 其中'00000'的个数为right函数的最后参数 ...
- Uva12504 Updating a Dictonary
这道题难度不大,主要是考察熟练运用C++的容器,字符串等操作. 另外注意特殊情况是否需要特殊处理.即当一个字典为空时,无论另一个字典是否有值,输出的结果都为No Change,这点需要注意一下. 另外 ...
- 第一节 MongoDB介绍及下载与安装
引言 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类 ...
- ThinkPHP之中的验证码的小示例
ThinkPHP之中已经封装好了验证码的调用,但是关于手册,缺失了HTML之中以及.实际操作之中的点击ajax就会刷新验证码ajax代码:现在分享一下:看客老爷们注意啦! 放大招啦!!!三分归元气-- ...
- 使APP消除上方手机消息提示栏(显示WIFI,信号格那栏)消失的方法
public void toggleFullscreen(boolean fullScreen) { // fullScreen为true时全屏,否则相反 WindowManager.LayoutPa ...
- Eclipse插件推荐:UCDetector: Unnecessary Code Detector
正如其名,检查不必要的代码. 下载地址为:http://sourceforge.net/projects/ucdetector/files/latest/download?source=files 官 ...
- C# 验证IP是否正确简易方法 源代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【转载】input 中 type='text' 的提交问题
原文链接:http://www.nowamagic.net/html/html_AboutInputSummit.php 有时候我们希望回车键敲在文本框(input element)里来提交表单(fo ...