SpringMVC04controller中定义多个方法
public class MyController extends MultiActionController {
// 新增 方法修饰符要是public
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("/WEB-INF/jsp/success.jsp", "result", "add()");
}
// 修改
public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("/WEB-INF/jsp/success.jsp", "result",
"update()");
}
}
创建对应的controller
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置一个处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<!--
因为我们的controller继承了MultiActionController,
在MultiActionController有个
/** Delegate that knows how to determine method names from incoming requests */
private MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver();
根据我们的请求中参数找到对应方法 的解析器
*:代表的就是我们的请求参数 也是对应的方法名
-->
<entry key="/user/*" value="myController"/>
</map>
</property>
</bean> <!-- 处理器 -->
<bean id="myController" class="cn.bdqn.controller.MyController">
<property name="methodNameResolver" ref="myResolver"></property>
</bean>
</beans>
mvc核心xml文件的配置
使用我们自身设置的解析器 来执行,不需要改变controller中的代码!只需要更改mvc核心xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置一个处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<!--
因为我们的controller继承了MultiActionController,
在MultiActionController有个
/** Delegate that knows how to determine method names from incoming requests */
private MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver();
根据我们的请求中参数找到对应方法 的解析器
*:代表的就是我们的请求参数 也是对应的方法名
-->
<entry key="/user/*" value="myController"/>
</map>
</property>
</bean> <!-- 设置解析器 -->
<bean id="myResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<!--
底层代码
Set explicit URL to method name mappings through a Properties object.
@param mappings Properties with URL as key and method name as value
public void setMappings(Properties mappings) {
this.mappings = mappings;
}
-->
<prop key="/user/adds">add</prop>
<prop key="/user/updates">update</prop>
</props>
</property>
</bean> <!-- 处理器 -->
<bean id="myController" class="cn.bdqn.controller.MyController">
<!--需要更改父类的解析器引用 变成我们自已定义的解析器 -->
<property name="methodNameResolver" ref="myResolver"/>
</bean> </beans>
mvc核心xml文件的设置
一旦我们设置了解析器,那么默认的


会执行我们设置的解析器
PropertiesMethodNameResolver

在浏览器中输入对应的url即可看到效果!
视图解析器
/**
01 .内部资源视图
public class MyController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// success 就是我们说的 逻辑试图名称
return new ModelAndView("success", "result", "add()");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 处理器 -->
<bean name="/myController" class="cn.bdqn.controller.MyController"/>
<!-- 使用我们默认的试图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- InternalResourceViewResolver 没有给前缀和后缀赋值 需要我们手动的赋值-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
02. 内部资源视图
public class MyController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// success 就是我们说的 逻辑试图名称
return new ModelAndView("myView", "result", "jstlView");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 处理器 -->
<bean name="/myController" class="cn.bdqn.controller.MyController"/>
<!--内部资源视图 -->
<bean id="myView" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/jsp/success.jsp"/>
<!-- <property name="url" value="http://www.jd.com"/> 不能跳转到外部的资源路径-->
</bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
</beans>
03.外部资源视图
public class MyController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// success 就是我们说的 逻辑试图名称
return new ModelAndView("jd");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 处理器 -->
<bean name="/myController" class="cn.bdqn.controller.MyController"/>
<!--外部资源视图 -->
<bean id="jd" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="http://www.jd.com"/>
</bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
</beans>
*/
部分代码
访问外部资源
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body> <%-- 这里的参数要和 配置文件中ParameterMethodNameResolver的paramName属性的value值相对应--%> <a href="myController?action=taobao">淘宝</a>
<a href="myController?action=jd">京东</a>
<a href="myController?action=baidu">百度</a> </body>
</html>
在webroot下创建index.jsp
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 参数名称 映射成 方法名称 -->
<bean id="parameter" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"/> <!-- 处理器 -->
<bean name="/myController" class="cn.bdqn.controller.MyController">
<property name="methodNameResolver" ref="parameter"/>
</bean> <!-- 配置视图解析器 解析xml文件 -->
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="classpath:MyViews.xml"/>
</bean> </beans>
springmvc-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--外部资源视图 -->
<bean id="jd" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="http://www.jd.com"/>
</bean>
<!--外部资源视图 -->
<bean id="taobao" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="http://www.taobao.com"/>
</bean>
<!--外部资源视图 -->
<bean id="baidu" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="http://www.baidu.com"/>
</bean> </beans>
MyViews.xml文件
public class MyController extends MultiActionController {
// 淘宝主页
public ModelAndView taobao(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// success 就是我们说的 逻辑试图名称
return new ModelAndView("taobao");
}
// 京东主页
public ModelAndView jd(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// success 就是我们说的 逻辑试图名称
return new ModelAndView("jd");
}
// 百度主页
public ModelAndView baidu(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// success 就是我们说的 逻辑试图名称
return new ModelAndView("baidu");
}
}
MyController代码
======================使用properties文件管理视图===========================
前台的页面还是使用的上面例子中的index.jsp
jd.(class)=org.springframework.web.servlet.view.RedirectView
jd.url=http://www.taobao.com taobao.(class)=org.springframework.web.servlet.view.RedirectView
taobao.url=http://www.taobao.com baidu.(class)=org.springframework.web.servlet.view.RedirectView
baidu.url=http://www.baidu.com
创建对应的properties文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 参数名称 映射成 方法名称 -->
<bean id="parameter" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"/> <!-- 处理器 -->
<bean name="/myController" class="cn.bdqn.controller.MyController">
<property name="methodNameResolver" ref="parameter"/>
</bean> <!-- 配置视图解析器 解析properties文件 -->
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<!--找到我们指定的properties文件 value就是代表的文件的名称 不需要加后缀名 -->
<property name="basename" value="myViews"/>
</bean> </beans>
配置文件xml文件的内容
=====================xml文件中同时存在多个视图解析器的执行顺序======================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 参数名称 映射成 方法名称 -->
<bean id="parameter" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"/> <!-- 处理器 -->
<bean name="/myController" class="cn.bdqn.controller.MyController">
<property name="methodNameResolver" ref="parameter"/>
</bean> <!-- 如果是定义了多个 视图解析器 默认是按照在xml文件中的配置顺序 执行的
如果不想使用默认的执行顺序 可以增加属性 order 设置一个正整数
数值越小 优先级越高
--> <!-- 解析xml文件 -->
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="classpath:MyViews.xml"/>
<property name="order" value="3"/>
</bean> <!-- 配置视图解析器 解析properties文件 -->
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<!--找到我们指定的properties文件 value就是代表的文件的名称 不需要加后缀名 -->
<property name="basename" value="myViews"/>
<property name="order" value="2"/>
</bean> </beans>
修改后的xml文件
SpringMVC04controller中定义多个方法的更多相关文章
- error LNK2005: “找到一个或多个多重定义的符号” 已经在 xxxx.obj 中定义 的解决方法
1 问题还原 这里我有三个源文件:Base.hpp, Base.cpp 和 main.cpp 在Base.hpp里面定义一个基类,注意,基类只包含构造函数和析构函数的声明,函数在Base.cpp里实现 ...
- JS中定义类的方法
JS中定义类的方式有很多种: 1.工厂方式 function Car(){ var ocar = new Object; ocar.color = "blue" ...
- JS中定义类的方法<转>
转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象 ...
- 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- 【Spring Framework】12种spring中定义bean的方法
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- Js中的假值_ES5中定义的ToBoolean方法强制类型转换后值为false
你不知道的Javascript(中)--ToBoolean javascript中的值可以分为以下两类: 1.可以被强制类型转换为false的值 2.其他(被强制类型转换为true的值) 假值---以 ...
- Android中定义接口的方法
1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...
- SpringMVC03controller中定义多个方法
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- php中定义数组的方法
1.PHP定义数组的格式 数组名=array(); 如:$aa=array();//这样就定义了一个数组, 之后给元素赋值: $aa[0]="9016"; $aa[1]=" ...
随机推荐
- 【USACO 2.3.2】奶牛家谱
[题目描述] 农民约翰准备购买一群新奶牛.在这个新的奶牛群中,每一个母亲奶牛都生两小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < 200).这些二叉 ...
- [置顶] css 背景透明,文字不透明,alpha滤镜,opacity,position:relative;
都知道,在alpha滤镜下,背景透明了,里面的文字也会跟随透明,我们可以设置内容的position为relative可以解决这个问题 但是在position为absolute这么做却没有效果,怎么解决 ...
- CentOS 7 之Helloworld with c
其实我也不知道是为了啥, 到了现在这种年纪还想学习Linux下的C语言编程.因为我一直就傻傻地认为机会是垂青有准备的人,也一直呆呆地认为活到老学到老.现在Android这么火,各种终端如雨后春笋,而这 ...
- 初涉JavaScript模式 (4) : 构造函数
什么是构造函数? 构造函数 是一种特殊的方法 主要用来在创建对象时初始化对象 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 特别的一个类可以有多个构造函数 可根据其参数个数的不 ...
- ASP.NET WEB API 2 框架揭秘 读书笔记(一)
第一章 概述 主要内容是介绍Web的基本概念,Restfull的基本概念及特性.最后介绍创建简单WebApi程序的步骤. Web的基本概念 IP/TCP协议簇分层,分为两种 链路层->网络层-& ...
- [javascript]事件冒泡处理
<!DOCTYPE html> <html> <head> <style type="text/css"> #box1 { widt ...
- 编写可维护的javascript代码--- 2015.11.22(注释)
1.单行注释 // 这是一句单行注释 2.多行注释 /* 这里是代码 */ /* 这里都是注释 1232132 */ java的注释风格 /* * 另一段注释 * 这段注释包含2 ...
- python之7-2类的继承与多态
类的继承的意思就如同父子关系一样,这个儿子继承了父亲的一切,但是在某些地方(属性)相同的时候,儿子的属性大于老子的属性(覆盖),最底层类,总会继承最接近它的那个类的属性init 类的多态总是和继承相连 ...
- python的bind函数
# -*- coding:utf-8 -*- class Functor(object): def __init__(self, func, index=0, *args, **kwargs): se ...
- Maven插件开发
Maven为我们提供了丰富的插件资源,使得开发调试过程中非常方便,可以满足大多数场景下的需求.当然有时候,我们也需要根据需求定制自己的插件.下面是在开发Maven插件时的一点备忘录,具体的开发流程请G ...