第一个SpringMVC项目

一、把相关jar包加进项目
项目jar包

二、配置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>SpringMCV_1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>

这里定义一个名为springmvc的servlet,根据规则添加名为springmvc-servlet.xml配置文件

三、配置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: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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

		<!-- 用于使用@ResponseBody后返回中文避免乱码 --> 
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
			<property name="messageConverters">
				<list>
					<bean class="org.springframework.http.converter.StringHttpMessageConverter">
						<!-- 用于避免响应头过大 --> 
						<property name="writeAcceptCharset" value="false" />
						<property name="supportedMediaTypes">
							<list>
								<value>text/html;charset=UTF-8</value>
							</list>
						</property>
					</bean>
				</list>
			</property>
		</bean>
	
		<!-- 自动扫描包 -->
        <context:component-scan base-package="com.leash.demo"></context:component-scan>
        
	    <!-- Spring MVC不处理静态资源 -->
	    <mvc:default-servlet-handler />
	
	    <!-- 支持mvc注解驱动 -->
	    <mvc:annotation-driven />
	    
		<!-- 定义无需Controller的url<->view直接映射 -->
		<mvc:view-controller path="/" view-name="redirect:/index.jsp" />
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
        
</beans>

四、创建HelloWorld控制器

package com.leash.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
//@RequestMapping(value="/hello",produces={"application/json;charset=utf-8"})
@RequestMapping("/hello")
public class HelloWorld
{
    @RequestMapping("/say")
	public String Say(Model model)
	{
    	model.addAttribute("message", "Hello World ! (中文)");
		return "hello";
	}
    
	@RequestMapping("/show")
	@ResponseBody
    public String Show()
    {
    	return "Hello World ! 中文";
    }
}

五、创建hello.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
	<h2>${message}</h2>
</body>
</html>

六、运行效果
1、返回字符串
字符串
2、跳转文件(hello.jsp)返回字符串
文件名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值