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

springMVC Helloword 入门程序

时间:2016-10-01 06:12:46      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

1  首先是在web.xml 中配置

	<servlet>
		<servlet-name>SpringMVC</servlet-name>
     	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     	<init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/classes/springmvc-servlet.xml</param-value>  
        </init-param>  
     	
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

2  添加配置文件

<?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:p="http://www.springframework.org/schema/p"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:util="http://www.springframework.org/schema/util"
 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/context http://www.springframework.org/schema/context/spring-context.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/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">
  


  <!-- 设置使用注解的类所在的jar包 -->
  <context:component-scan base-package="com.helloword"></context:component-scan>

    <bean name="/helloWorld" class="com.helloword.HelloWorldController" />  

    <!-- ViewResolver -->  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>
</beans>

  然后写Controller

package com.helloword;

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 HelloWorldController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest requst, HttpServletResponse response) throws Exception {
		
		System.out.println("这是我的第一个SpringMVC程序......");
		
		ModelAndView mav = new ModelAndView("/helloWord");
		
		mav.addObject("message", "我是来自服务器的消息");
		
		return mav;
	}

}

 最后是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>时间是检验真理的唯一标砖</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">
  </head>
  
  <body>
                 甘心安于现状 : ${message}<br>
  </body>
</html>

  

springMVC Helloword 入门程序

标签:

原文地址:http://www.cnblogs.com/dbqjava/p/5925373.html

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