springmvc helloworld

import spring-framework-4.0.6.RELEASE
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
            http://www.springframework.org/schema/aop/spring-aop-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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
         <bean name="/hello.do" class="com.web.helloworld.HelloController"></bean>
         <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>
         <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
         <mvc:annotation-driven />  
         <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  
         <context:component-scan base-package="com" />  
         <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->  
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  p:suffix=".jsp" />  
    </beans>  

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>helloworld</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <!-- 应用上下文配置文件 -->  
        <param-value>classpath*:spring-servlet.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
	<servlet>  
		<servlet-name>springmvc</servlet-name>  
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
	</servlet>
	<servlet-mapping>  
		<servlet-name>springmvc</servlet-name>  
		<url-pattern>*.do</url-pattern>  
	</servlet-mapping>
</web-app>

com.web.helloworld.HelloController
  package com.web.helloworld;

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

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



public class HelloController extends AbstractController{

	@Override
protected ModelAndView handleRequestInternal(HttpServletRequest Request,HttpServletResponse Response) throws Exception {
		String hello = Request.getParameter("hello");
		System.out.println("...:" + hello );
		ModelAndView mav = new ModelAndView("index");
		mav.addObject("helloworld", "hello" + hello);
		return mav;
	}

}


jsp
WebContent/
hello.jsp
index.jsp

你可能感兴趣的:(springMVC)