springMVC(5) springMVC一个controller写多个方法

上面的例子都是实现Controller,实现handleRequest方法,我们想像struts2那样一个action里有多个方法怎么办?

可以继承MultiActionController类,在类里写多个方法。spring-servlet.xml里需要配置ParameterMethodNameResolver

MultiController:

package com.xdy.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 MultiController extends MultiActionController{

	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("---add---");
		return new ModelAndView("/welcome","method","add");
	}

	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("---update---");
		return new ModelAndView("/welcome","method","update");
	}
}

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean name="/multi" class="com.xdy.controller.MultiController">
		<property name="methodNameResolver" ref="parameterMethodNameResolver"></property>
	</bean>
	
	<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
		<property name="paramName" value="action"></property>
	</bean>
	
	<!-- 配置解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
</beans>

welcome.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>springMVC中一个controller中写多个方法</title>
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  <body>
    <h1>一个controller中写多个方法</h1>
    	方法名称是:${method }
  </body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个简单的示例,演示如何在Spring MVC后端和AngularJS前端之间进行通信。假设我们有一个简单的应用程序,它允许用户查看和编辑他们的个人信息。 后端: 在后端,我们使用Spring MVC框架来处理HTTP请求和响应。我们将创建一个RESTful API,它将提供以下端点: - GET /user/{id}:获取某个用户的个人信息 - PUT /user/{id}:更新某个用户的个人信息 我们的UserController将负责处理这些请求,并使用UserService来访问数据库。 ``` @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User getUser(@PathVariable Long id) { return userService.getUser(id); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public void updateUser(@PathVariable Long id, @RequestBody User user) { userService.updateUser(id, user); } } ``` 前端: 在前端,我们将使用AngularJS框架来构建我们的用户界面。我们将使用路由器来处理不同的页面,并使用控制器来处理与后端的通信。 我们将创建两个页面:一个用于查看用户信息,另一个用于编辑用户信息。我们将使用$http服务来发出HTTP请求,并使用ng-model指令来绑定表单字段。 ``` // app.js var app = angular.module('myApp', ['ngRoute']); app.config(function($routeProvider) { $routeProvider .when("/", { templateUrl : "view.html", controller : "viewController" }) .when("/edit", { templateUrl : "edit.html", controller : "editController" }); }); app.controller('viewController', function($scope, $http) { $http.get("/user/1") .then(function(response) { $scope.user = response.data; }); }); app.controller('editController', function($scope, $http) { $http.get("/user/1") .then(function(response) { $scope.user = response.data; }); $scope.updateUser = function() { $http.put("/user/1", $scope.user) .then(function(response) { // user updated successfully }); }; }); ``` ``` <!-- view.html --> <h1>{{user.name}}</h1> <p>{{user.email}}</p> <a href="#/edit">Edit</a> ``` ``` <!-- edit.html --> <form> <input type="text" ng-model="user.name"> <input type="text" ng-model="user.email"> <button ng-click="updateUser()">Save</button> </form> ``` 在此示例中,我们只是针对单个用户进行了操作。在实际应用程序中,我们需要处理多个用户并使用身份验证和授权来保护敏感信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值