SpringMVC

Posted 小企鹅推雪球!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC相关的知识,希望对你有一定的参考价值。

Spring MVC多动作控制器

  1. MultiActionController类用于在单个控制器中分别映射多个URL到对应的方法。
package com.yiibai.springmvc;

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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("home");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}

URL映射配置文件

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
   <bean name="/home.html" class="com.yiibai.springmvc.UserController" /> 
   <bean name="/user/*.html" class="com.yiibai.springmvc.UserController" />
  1. 对于/home.html请求,DispatcherServlet将请求转发到UserController类的 home()方法。
  2. 对于user/add.htmlDispatcherServlet将请求转发到UserController类的 add()方法。
  3. 对于user/remove.htmlDispatcherServlet将请求转发到UserController类的 remove()方法。

Spring MVC多动作控制器样例

  1. 创建一个名称为 MultiActionController的动态WEB项目
  2. com.yiibai.springmvc包下创建一个Java类UserController
  3. jsp子文件夹下创建两个视图文件:home.jsp 和 user.jsp

UserController.java

package com.yiibai.springmvc;
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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("home");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}

MultiActionController-servlet.xml

<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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/"/>
      <property name="suffix" value=".jsp"/>
   </bean>

   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
   <bean name="/home.html" 
      class="com.yiibai.springmvc.UserController" /> 
   <bean name="/user/*.html" 
      class="com.yiibai.springmvc.UserController" />    
</beans>

home.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home</title>
</head>
<body>
<body>
<a href="user/add.html" >add()方法</a> <br>
<a href="user/remove.html" >remove()方法</a>
</body>
</html>

user.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
   <h2>${message}</h2>  
</body>
</html>

Spring MVC属性方法名称解析器

  1. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法
package com.yiibai.springmvc;

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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "主页面(Home方法)");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "添加(Add方法)");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "删除(Remove方法)");
      return model;
   }
}

URL映射配置文件

<bean class="com.yiibai.springmvc.UserController">
   <property name="methodNameResolver">
      <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
         <property name="mappings">
            <props>
               <prop key="/user/home.html">home</prop>
               <prop key="/user/add.html">add</prop>
               <prop key="/user/update.html">update</prop>      
            </props>
         </property>
      </bean>
   </property>
</bean>
  1. 对于/user/home.html请求,DispatcherServlet将请求转发到UserController类的 home()方法
  2. 对于user/add.htmlDispatcherServlet将请求转发到UserController类的add()方法。
  3. 对于user/remove.htmlDispatcherServlet将请求转发到UserController类的 remove()方法

Spring MVC属性方法名称解析器样例

  1. 创建一个名称为 PropertiesMethodNameResolver 的动态WEB项目。
  2. 在 com.yiibai.springmvc 包下创建一个Java类UserController。
  3. 在jsp子文件夹下创建一个视图文件:user.jsp。

UserController.java

package com.yiibai.springmvc;
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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "主页面(Home方法)");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "添加(Add方法)");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "删除(Remove方法)");
      return model;
   }
}

PropertiesMethodNameResolver-servlet.xml

<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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/"/>
      <property name="suffix" value=".jsp"/>
   </bean>

   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> 
      <property name="caseSensitive" value="true" />
   </bean>
   <bean class="com.yiibai.springmvc.UserController">
      <property name="methodNameResolver">
      <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
         <property name="mappings">
            <props>
               <prop key="/user/home.html">home</prop>
               <prop key="/user/add.html">add</prop>
               <prop key="/user/remove.html">remove</prop>
            </props>
         </property>
      </bean>
      </property>
   </bean>  
</beans>

user.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
   <h2>${message}</h2>  
</body>
</html>

Spring MVC参数方法名称解析器

  1. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法
package com.yiibai.springmvc;
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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}

URL映射配置文件

<bean class="com.yiibai.springmvc.UserController">
   <property name="methodNameResolver">
      <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
         <property name="paramName" value="action"/>
      </bean>
   </property>
</bean>
  1. 对于/user/*.html?action=home请求,DispatcherServlet将请求转发到UserController类的 home()方法。
  2. 对于/user/*.html?action=addDispatcherServlet将请求转发到UserController类的 add()方法。
  3. 对于/user/*.html?action=removeDispatcherServlet将请求转发到UserController类的 remove()方法

Spring MVC参数方法名称解析器样例

  1. 创建一个名称为 ParameterMethodNameResolver 的动态WEB项目
  2. 在 com.yiibai.springmvc 包下创建一个Java类UserController
  3. 在jsp子文件夹下创建一个视图文件:user.jsp

UserController.java

package com.yiibai.springmvc;
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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "主页面(Home)");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "添加(Add方法)");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "删除(Remove方法)");
      return model;
   }
}

ParameterMethodNameResolver-servlet.xml

<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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/"/>
      <property name="suffix" value=".jsp"/>
   </bean>

   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> 
      <property name="caseSensitive" value="true" />
   </bean>
   <bean class="com.yiibai.springmvc.UserController">
      <property name="methodNameResolver">
         <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
            <property name="paramName" value="action"/>
         </bean>
      </property>
   </bean>
</beans>

user.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User.jsp Page</title>
</head>
<body>
   <h2>${message}</h2>  
</body>
</html>

Spring MVC可参数化的视图控制器

  1. 可参数化视图允许将请求映射到网页。
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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Home");
      return model;
   } 
}

URL映射配置文件

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   <property name="mappings">
      <value>
         index.html=userController
      </value>
   </property>
</bean>
<bean id="userController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
   <property name="viewName" value="index"/>
</bean>
  1. 对于/index.html请求,DispatcherServlet会将请求转发到UserController控制器,并将viewName设置为index.jsp。

Spring MVC可参数化的视图控制器样例

  1. 创建一个名称为 ParameterizableViewController 的动态WEB项目
  2. 在 com.yiibai.springmvc 包下创建一个Java类UserController
  3. 在jsp子文件夹下创建一个视图文件:index.jsp

UserController.java

package com.yiibai.springmvc;
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 UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Home");
      return m

以上是关于SpringMVC的主要内容,如果未能解决你的问题,请参考以下文章

springmvc 分页查询的简单实现

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]

Spring MVC 教程

spring 国际化 js怎么设置

AngularJS ——ngResourceRESTful APIs 使用

第二周作业