你的位置:首页 > Java教程

[Java教程]学习springMVC实例1——配置和跳转到HelloWorld


本文让大家迅速掌握springMVC的使用方法,以最简单的方式理解此框架

一、用eclipse新建一个web项目,命名为springMVC1,tomcat的端口号为9090

二、在WEB-INF目录下的lib文件夹中添加所需要的jar包

  上面所添加的jar包,只要从网上下载都是都有的

三、编辑web.

  

 1 <??> 2 <web-app ="http://www.w3.org/2001/ ="http://java.sun.com/ ="http://java.sun.com/ xsi:schemaLocation="http://java.sun.com/ id="WebApp_ID" version="2.5"> 3  <display-name>springMVC1</display-name>
    <!-- 标签<welcome-file>就是直接访问http://localhost:9090/springMVC1此项目时直接跳转到设置的页面--> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file>10 <welcome-file>default.jsp</welcome-file>11 </welcome-file-list>12 13 <servlet>
      <!-- 下面的springMVC和DispatcherServlet就是设置让springMVC获取到用户的所有请求-->14 <servlet-name>springMVC</servlet-name>15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>16 <init-param>
      <!-- 下面的contextConfigLocation和spring-servlet.
17 <param-name>contextConfigLocation</param-name>
18        <param-value>classpath*:config/spring-servlet.</param-value>19      </init-param>20    <load-on-startup>1</load-on-startup>21  </servlet>22  23  <servlet-mapping>
      <!--一定要和上面配置的<servlet-name>名称一致,过滤上面获取的用户请求,下面设置就意味着接收所有的请求-->24 <servlet-name>springMVC</servlet-name>25 <url-pattern>/</url-pattern>26 </servlet-mapping>29 </web-app>

四、编写控制器,在src目录下,新建包com.tgb.web.controller,在此包下新建java类HelloWorldController.java

 1 package com.tgb.web.controller; 2  3 import java.util.HashMap; 4 import java.util.Map; 5  6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 import org.springframework.web.servlet.ModelAndView;10 import org.springframework.web.servlet.mvc.Controller;11 12 public class HelloWorldController implements Controller {13 14   @Override15   public ModelAndView handleRequest(HttpServletRequest arg0,16       HttpServletResponse arg1) throws Exception {    17     System.out.println("------这是控制方法-----");18     Map<String,Object> map=new HashMap<String,Object>();19     map.put("hello1", "hello1");20     map.put("hello2", "hello2");21     map.put("hello3", "hello3");22     String hello = "你好啊";23     String url="/welcome";24     return new ModelAndView(url,"map",map);25   }27 }

根据springMVC规范HelloWorldController要实现接口Controller和方法,这里大家暂时先这么用,最原始的访问方法,后面会和大家说全部用注解的形式访问,实现的方法返回的是ModelAndView,它可以直接returna new ModelAndView(url,"map",map),url就是跳转的页面地址,map就是传递到jsp页面的数据对象

五、编写jsp页面,在WEB-INF目录下,新建jsp文件夹,新建welocme.jsp文件

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2   pageEncoding="UTF-8"%> 3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head>10 <body>11   这是我的第一个jsp页面!!!!!!!++++++++++12   <br/>13   <h>传递的数据</h>14   ${hello}15   ${map.hello2}16   <div>17   18     <c:forEach items="${map}" var="m">19       ${m.key }======${m.value }<br/>20     </c:forEach>21   </div>22 </body>23 </html>

 六、以上有了控制层和页面,那么我怎么让springMVC知道我的这些资源呢,并且能够请求?那么解决方法就是上上面提到的springMVC的上下文文件spring-servlet.

 

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans"  3  ="http://www.springframework.org/schema/context"  4  ="http://www.springframework.org/schema/p"  5  ="http://www.springframework.org/schema/mvc"  6  ="http://www.w3.org/2001/  7  xsi:schemaLocation="http://www.springframework.org/schema/beans  8    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  9    http://www.springframework.org/schema/context 10    http://www.springframework.org/schema/context/spring-context.xsd 11    http://www.springframework.org/schema/mvc 12    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">13    14   15   <!--name="/test1/helloworld" 就是要访问控制器的路径,然后进入这个控制方法-->16   <bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController" />27   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">28     <property name="prefix" value="/WEB-INF/jsp"></property>29     <property name="suffix" value=".jsp"></property>30   </bean>31 </beans> 

 

七、部署编辑

  直接在浏览器的地址栏里输入http://localhost:9090/springMVC1/test1/helloworld

 八、控制器里包含多个方法

  1.新建包含多个方法的控制器multiController.java

  

package com.tgb.web.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 {  //定义add的方法,在url中访问http://localhost:9090/springMVC1/test1/multi?action=add  public ModelAndView add(HttpServletRequest request,HttpServletResponse respose){    System.out.println("add");    String method="add";    return new ModelAndView("/multi","method",method);  }  //定义update方法,在url中访问http://localhost:9090/springMVC1/test1/multi?action=update  public ModelAndView update(HttpServletRequest request,HttpServletResponse respose){    System.out.println("update");    String method="update";    return new ModelAndView("/multi","method",method);  }  }

  2.新建多方法跳转的jsp,multi.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>控制器中多个方法跳转的页面</title></head><body>  <h>控制器中多个方法跳转的页面</h>  本次方法是${method }  <br/>  <h>传递的数据</h>  ${hello}  ${map.hello2}  <div>      <c:forEach items="${map}" var="m">      ${m.key }======${m.value }<br/>    </c:forEach>  </div></body></html>

  3.在spring-servlet.

1 <bean name="/test1/multi" class="com.tgb.web.controller.MultiController" >2     <property name="methodNameResolver">3       <ref bean="paramMethodResolver"/>4     </property>5   </bean>6   <!--加载springMVC的多方法解析包,标签<property>中name固定为paramName,value的值是可以改变的,在访问路径里需要用到,例如http://localhost:9090/springMVC1/test1/multi?action=add-->7   <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" >8     <property name="paramName" value="action"></property>9   </bean>