首页 > 代码库 > SpringMVC03controller中定义多个方法
SpringMVC03controller中定义多个方法
<%@ 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="http://www.mamicode.com/styles.css"> --> </head> <body> <%-- 这里的参数要和 配置文件中ParameterMethodNameResolver的paramName属性的value值相对应--%> <a href="hello?action=add">新增</a> <a href="hello?action=update">修改</a> <a href="hello?action=del">删除</a> <a href="hello?action=find">查询</a> </body> </html>
<?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"> <!-- ParameterMethodNameResolver:会根据前台请求的参数名称 对应到指定的controller中 方法名称 前台的请求中 必须携带参数 action=xxx 如果需要自己定义 则需要更改paramName的value值 --> <bean id="parameter" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action"/><!-- 可以省略 因为是默认值 --> </bean> <!-- 处理器 --> <bean name="/hello" class="cn.bdqn.controller.MyController"> <property name="methodNameResolver" ref="parameter"/> </bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
public class MyController extends MultiActionController {
// 新增
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("success", "result", "新增成功界面");
}
// 修改
public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("success", "result", "修改成功界面");
}
// 删除
public ModelAndView del(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("success", "result", "删除成功界面");
}
// 查看
public ModelAndView find(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("success", "result", "查看成功界面");
}
}
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--配置springmvc的核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置spring配置文件的位置 以及名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<body> ${result} </body>
SpringMVC03controller中定义多个方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。