码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC入门配置及helloworld实例

时间:2016-06-17 14:09:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1. 新建web project 

2. 往lib里copy必须的jar

3. 改写web.xml    

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

 

4. 在WEB-INF下新建 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:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    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/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
     
     
    <!-- ViewResolver 视图解析器 --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean>
     
</beans>

  

5. src下新建package: com.tgb.web.controller   class:  HelloWorldController.java      

package com.tgb.web.controller;

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

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


public class HelloWorldController implements org.springframework.web.servlet.mvc.Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		System.out.println("---hello wly--------");
		return new ModelAndView("/welcome");
	}
}

 

6. 新建welcome.jsp, 在WEB-INF下

7. 修改配置 文件 springMVC-servlet.xml

<bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController"></bean>

  

8. IE测试:  http://localhost:8080/springMVC1/test1/helloworld

springMVC入门配置及helloworld实例

标签:

原文地址:http://www.cnblogs.com/wujixing/p/5593807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!