SpringMVC_001 第一个SpringMVC程序

今天我们来学习第一个SpringMVC程序

一.配置开发方式

(1)首先建立一个SpringMVC  web程序

(2)导入jar包

(3)建立UserController.java

package com.zk.UserController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class UserController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		// 接受请求,接受参数,验证参数
		// 封装参数,调用业务方法
		// 返回视图
		ModelAndView mv=new ModelAndView();
		//设置页面回显数据
		mv.addObject("hello", "凤姐喜欢你");
		//指定跳转视图,返回物理视图
		//mv.setViewName("/jsp/index.jsp");
		//返回逻辑视图
		mv.setViewName("index");
		return mv;
	}

}

(4)配置web.xml,在web.xml配置DispatcherServlet

<?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">
  <display-name>Spring_001</display-name>
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 
  Spring 默认加载SpringMVC的配置文件,这个需要满足一下规则:
                命名规则:Servlet-name-servlet.xml========>SpringMVC-servlet.xml
                 路径规范SpringMVC-serlvet.xml必须放在WEB-INF下
   -->
   <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/config/springmvc.xml</param-value>   
   </init-param>
  </servlet>	
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

(5)配置springmvc.xml,放在config文件夹下;

<?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:aop="http://www.springframework.org/schema/aop"
	 xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
         <!-- 配置处理器映射器,SpringMVC的默认处理器映射器
         BeanNameUrlHandlerMapping:根据bean(自定义Contoller)的name属性的url去寻找handler(Action:controller)
          -->
         <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
         <!-- 配置处理器适配器:负责执行Controller,springMVC默认处理器适配器SimpleControllerHandlerAdapter -->
     	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
         <!-- 配置自定义UserController -->
         <bean name="/hello.do" class="com.zk.UserController.UserController"></bean>
         <!-- 配置SpringMVC视图解析器:解析逻辑视图
              后台返回逻辑视图,
              视图解析器解析出真正的物理视图:前端+逻辑视图+后缀=====/WEB-INF/jsps/index.jsp
          -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"></property>
         <property name="suffix" value=".jsp"></property>
         </bean>
</beans>

(6)jsp目录下,index.jsp

<%@ 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="Content-Type" content="text/html;charset=utf-8" />
	<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="styles.css">
	-->
  </head>
  
  <body>
    ${hello }
  </body>
</html>

 

 运行程序后,输入url:http://ms-20170731tkkc:8080/SpringMVC_001/hello.do,得到运行结果为:

二.注解开发方式

第二种开发方式为注解开发,使用@Controller加@RequestMapping进行注解开发。

代码如下:

UserController.java

package com.zk.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller//<bean id="UserController" class="UserController路径">
public class UserController {
	//访问路径注意更改
	//请求映射value="/hello.do",
	//@RequestMapping("hello")
	//@RequestMapping("/hello.do")
	//@RequestMapping(value="/hello.do")
	//@RequestMapping(value="/hello.do",method=RequestMethod.GET)
	@RequestMapping(value="/hello.do",method={RequestMethod.GET,RequestMethod.POST})
	public String myHello(){
		return "hello";
	}	
}

requestMapping(value=”/hello.do”,method=RequestMethod.POST)

浏览器直接访问,a标签都是get请求

表单提交(指定post)ajax指定post提交,post提交。

 

Spring.xml配置文件如下:

<?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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
         <context:component-scan base-package="com.zk.Controller"></context:component-scan>
		
		<!-- 配置注解处理器映射器 
			功能:寻找执行类Controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
		
		<!-- 配置注解处理器适配器 
			功能:调用controller方法,执行controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
        
         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"></property>
         <property name="suffix" value=".jsp"></property>
         </bean>  
</beans> 

web.xml

<?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">
  <display-name>Spring_006</display-name>
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 
  Spring 默认加载SpringMVC的配置文件,这个需要满足一下规则:
  命名规则:Servlet-name-servlet.xml========>SpringMVC-servlet.xml
  路径规范SpringMVC-serlvet.xml必须放在WEB-INF下
   -->
   <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/config/springmvc.xml</param-value>   
   </init-param>
  </servlet>	
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'Hello.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="styles.css">
	-->

  </head>
  
  <body>
    Hello,UserController <br>
  </body>
</html>

  运行结果如下:

posted @ 2019-08-18 00:35  leagueandlegends  阅读(178)  评论(0编辑  收藏  举报