`
schy_hqh
  • 浏览: 542399 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

spring_MVC_01

 
阅读更多

1.下载spring,解压,拷贝libs目录下的jar包到项目中
2.配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">

	<servlet>
		<servlet-name>first</servlet-name>
		<!-- 配置springMVC提供的分发器,相当于一个servlet的总控制器,负责请求的分发 -->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>first</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

 
3.在WEB-INF目录下,新建一个xml文件,用来对web.xml中配置的DispatcherServlet进行详细说明
名称命名要求:first-servlet.xml (first对应DispatcherServlet所配置的名称)

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 配置需要被扫描的包 -->
    <context:component-scan base-package="com.hqh.student"/>
    
    <!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/jsp/"/>
    	<property name="suffix" value=".jsp"/>
    </bean>
</beans>

 
4.编写Controller控制器

package com.hqh.student.controller;

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

//配置为控制器
@Controller
public class FirstController {
	
	//配置请求映射策略
	@RequestMapping(value={"/hello"})
	public String hello() {
		System.out.println("FirstController.hello()");
		return "hello";
	}
}

 
5.在WEB-INF下新建jsp目录,新建hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>hello spring mvc</h1>
</body>
</html>

 
6.部署项目到Tomcat,启动并访问
http://localhost:8080/spring_mvc_01/hello

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics