springmvc入门

引入jar包:
spring-web-4.1.7.RELEASE.jar
spring-webmvc-4.1.7.RELEASE.jar
新建动态工程
jsps文件夹放在WEB-INF下面
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
//springmvc.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/jee    
 http://www.springframework.org/schema/jee/spring-jee.xsd   
 http://www.springframework.org/schema/lang    
 http://www.springframework.org/schema/lang/spring-lang.xsd   
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context.xsd   
 http://www.springframework.org/schema/tx    
 http://www.springframework.org/schema/tx/spring-tx.xsd   
 http://www.springframework.org/schema/util    
 http://www.springframework.org/schema/util/spring-util.xsd   
 http://www.springframework.org/schema/mvc    
 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
 <!-- 配置处理器映射器  第一种-->
 <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> -->
 
 <!-- 配置集中管理的映射器 第二种 集中声明 -->
 <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 <property name="mappings">
 <props>
 	<prop key="abc.do">userController</prop>
 	<prop key="sf.do">userController</prop>
 	<prop key="test.do">userController</prop>
 </props>
 </property>
 </bean> -->
 
 <!--根据类名访问  第三种 -->
 <!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> -->
 
 
 <!--配置处理器适配器  -->
 <!--SimpleControllerHandlerAdapter:controller要实现controller接口  -->
 <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> -->

 <!--HttpRequestHandlerAdapter要实现HttpRequestHandler  -->
<!--  <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
 	<bean id="userController" name="/hello.do" class="com.springmvc.controller.UserController"></bean> -->
 
 <!--******************************************************************************************************  -->
 <!-- 使@controller注解生效 -->
<!--  <context:component-scan base-package="com.springmvc.controller"></context:component-scan>	 -->
 <!-- 配置注解处理器映射器 -->
<!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
<!-- 配置注解处理器适配器 -->
<!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
<!--******************************************************************************************************  -->

<!-- 使@controller注解生效 -->
 <context:component-scan base-package="com.springmvc.controller"></context:component-scan>	
<mvc:annotation-driven></mvc:annotation-driven>    <!--开启注解模式  -->
 <!--配置视图解析器  -->	
 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 	<property name="prefix" value="/WEB-INF/jsps/"></property>
 	<property name="suffix" value=".jsp"></property>
 	</bean>
 </beans>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>Insert title here</title>
</head>
<body>
<%-- ${hello} --%>
hello world test
</body>
</html>

package com.springmvc.controller;

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

/*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 arg0, HttpServletResponse arg1) throws Exception {
//		ModelAndView mv = new ModelAndView();
//		mv.addObject("hello", "hello world");
//		// 物理视图(绝对路径)
//		// mv.setViewName("/WEB-INF/jsps/hello.jsp");		
//		//逻辑视图(springmvc.xml中需要配置逻辑视图解析器)
//		mv.setViewName("hello");
//		return mv;
//	}
//
//}

/***************注解************/
@Controller
//给类定义根路径,加根路径是在开发的过程当中,Controller会很多,
//区分不同controller里的方法
@RequestMapping("/userController")
public class UserController{
//	@RequestMapping("hello")//requestMapping("")作为执行方法的请求路径
//	@RequestMapping(value="hello")
	//用get方法规定请求
//	@RequestMapping(value="hello",method=RequestMethod.GET)
	//method=RequestMethod.Post 请求路径只支持Post
	@RequestMapping(value="hello",method= {RequestMethod.POST,RequestMethod.GET})
	public String testHello() {
		System.out.println("you are beautiful");
		return "hello";
	}
/*void类型不可	
@RequestMapping("add")
	public void add() {
		System.out.println("add");
		
	}*/
}

package com.springmvc.controller;

import java.io.IOException;

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

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;

public class HttpController implements HttpRequestHandler {

	public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		
		
	}
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值