首页 技术 正文
技术 2022年11月15日
0 收藏 709 点赞 2,599 浏览 3698 个字

出处:http://blog.csdn.net/xuewenke/article/details/23895999

springmvc2 一个控制器写多个方法(非注解方式)

分类: spring 2014-04-17 02:50 318人阅读 评论(0) 收藏 举报

web.xml 的配置。 其实web.xml 的配置和之前的没有什么区别  主要是DispatcherServlet 的配置,他是springvc的核心,是前端拦截器,主要负责拦截请求,然后在分派。这次配置时 springmvc的配置文件不是使用默认的路径,而是自己指定的路径 。

从这点我们可以看出,web.xml 只要加载 DispatcherServlet  servlet类就可以了 其余的工作就全部交给他去完成了。

web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance http://www.springmodules.org/schema/cache/springmodules-cache.xsd http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
“>
<display-name>Springmvc1</display-name>

<!– springmvc 的配置 配置核心的东西 分派器 –>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!–指明加载方式 –>
<load-on-startup>1</load-on-startup>
<!– 指明springMvc 配置文件的路径 –>
<!– 默认的路径是在 WEB-INF 下面 加入你的springmvc 配置文件在 默认路径下 就可以不配 而且默认的文件名称是 springMvc-servlet.xml –>

<!– 当你不使用默认路径的时候 可以用一下方法来配置springmvc 的配置文件 –>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-servlet.xml</param-value>
</init-param>

</servlet>

<!– springMvc 的映射 –>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

springmvc 文件的配置   spring-servlet.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans:beans xmlns=”http://www.springframework.org/schema/mvc”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:beans=”http://www.springframework.org/schema/beans”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.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”>
<!– 配置视图解析器 –>
<beans:bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
p:prefix=”/WEB-INF/” p:suffix=”.jsp”>
<!– 配置视图的属性 前缀和后缀 –>

</beans:bean>

<!– 配置相应的controller –>
<!– 这个name 其实就相当于你要提交的url 当你提交的url 是 /hello的时候 你的请求 就会 被DispatcherServlet 拦截器拦截下来 –>
<!– 然后他在根据那个控制器是负责处理/hello 请求的 然后找到他,然后就由他来处理这个请求的相应的任务。 –>
<beans:bean name=”/method” class=”com.seven.controller.MutilController”>

<beans:property name=”methodNameResolver”>
<beans:ref bean=”paramMethodResolver”/>
</beans:property>
</beans:bean>

<!– 配置参数名称解析器 –>

<beans:bean id=”paramMethodResolver” class=”org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver”>
<beans:property name=”paramName” value=”a”/>

</beans:bean>

</beans:beans>

控制器文件

package com.seven.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MutilController extends MultiActionController{

// 增加的函数
public ModelAndView add(HttpServletRequest request,HttpServletResponse response) / / 里面的 request 和response 必须要有 否则不能访问
{
System.out.println(“add”);
return new ModelAndView(“/test”,”method”,”add”);

}

// 更新函数
public ModelAndView update (HttpServletRequest request,HttpServletResponse response)
{
System.out.println(“update”);
return new ModelAndView(“/test”,”method”,”update”);
}

}

然后自己在写一个文件名是 test.jsp 的文件  放在 WEB-INF 文件夹下

然后请求的连接是  localhost:8080/Springmvc2/method?a=add   他会根据你带的参数而去调用不同的函数

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902