SpringMVC学习心得

来源:互联网 发布:知乎网名 编辑:程序博客网 时间:2024/05/19 11:18

最近项目比较清闲,抽出时间来学习一下传说中的SpringMVC,讲学习心得记录如下:

导入需要的jar文件

在web.xml中配置关键映射文件:

<servlet>
   <servlet-name>Hello</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <!-- 加载任意配置文件 -->
     <param-value>classpath*:/config/*.xml</param-value>
    </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>Hello</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>

注意:在此配置中,拦截全部配置,并且加载所有classpath目录下面的配置文件

然后创建controller的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 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 name="/test1/helloworld" class="org.calm.controller.HelloController" />
 <!-- 多个方法的实体 -->
 <bean name="/test2/manyMethod" class="org.calm.controller.ManyMethodController">
  <property name="methodNameResolver">
   <ref bean="paramMethodResolver" />
  </property> 
 </bean>

 <!-- 编写多个方法 -->
 <bean id="paramMethodResolver"
  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="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>
</beans>         

编写完成controller的映射文件之后,就开始编写controller的代码,当然根据上面编写的配置文件总可以看出来,它实现了两种传值方式:访问一个方法、访问多个方法。

开始编写最原始的helloworld:

public class HelloController implements Controller{

 @Override
 public ModelAndView handleRequest(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  System.out.println("请求了。。");
  String value="fuck";
  
  //向页面传递Map
  Map<String,Object> map=new HashMap<String,Object>();
  map.put("map1", "value1");
  map.put("map2", "value2");
  map.put("map3", "value3");
  return new ModelAndView("Hello","map",map);
 }
}
controller代码编写完成之后在页面上就可以取出来了

另外一个方法就是一个controller中可以编写多个方法,实体配置如上就不多咀笔了,重点是controller的代码。之前编写helloworld中,是实现的Controller接口;在编写一个controller中编写多个方法是继承MultiActionController:

public class ManyMethodController extends MultiActionController {

 public ModelAndView add(HttpServletRequest request,
   HttpServletResponse response) {
  System.out.println("-----add-------");
  return new ModelAndView("manyMethod", "methodName", "add");

 }

 public ModelAndView update(HttpServletRequest request,
   HttpServletResponse response) {
  System.out.println("-----update-------");
  return new ModelAndView("manyMethod", "methodName", "update");

 }
}

让后可以在页面上取出来:

在使用一个controller编写多个方法时,遇到一个小问题:为什么编写的方法一定要传递request和response参数呢?编写普通的方法就不行呢?