初识 Spring(09)---(搭建SpringMVC项目)

SpringMVC(创建项目)

步骤:1.创建外部项目

           2.导入所需的SpringMVC jar包

           3.在 web.xml 中配置文件(servlet)

文件目录:

  

配置文件:springmvc.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"
	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">

	<context:component-scan base-package="com.neuedu.springmvc.controller"></context:component-scan>
	<!-- 配置视图解析器:根据用户返回的字符串(逻辑视图),找到一个真正的jsp页面(物理视图) -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</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>springmvc-01</display-name>
   <!--  配置springmvc前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
		<!-- 从那个配置文件路径下读取spring的配置文件 -->
			<param-name>contextConfigLocation</param-name>
			<!-- springmvc的配置文件该存放在那个路径下面 -->
			<param-value>classpath:springmvc.xml</param-value>
			
		</init-param>
		<load-on-startup>1</load-on-startup>
		<!-- 在启动Tomcat时,生成servlet对象 -->
	</servlet>

	
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- servlet 要映射那些路径, / 表示在地址栏里面所有url都会被DispatcherServlet接收 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  
</web-app>

Hello World.java

package com.neuedu.springmvc.controller;

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

import com.sun.xml.internal.fastinfoset.util.PrefixArray;

@Controller
public class HelloWorld {
	/*视图解析器根据方法返回的字符串
	真正的视图地址:Prefix + returnVal + suffix (前缀+返回值+后缀)
	即/WEB-INF/views/success.jsp*/
	@RequestMapping("/hello")
	/*请求的映射,当你在控制台上输入hello时,DispatcherServlet收到url地址后,会开始寻找
	扫描所有的contrlloer,都到spring容器中去了,所以在spring容器中找是否有方法上面加的
	RequestMapping注解跟url地址相匹配,若能够执行则输出hello world*/
	public String hello() {
		System.out.println("hello world");
		return "success";
	}
}

页面:

success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
	<h4>Success page</h4>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
	<a href="hello"> Hello Index</a>
</body>
</html>

输出:点击超链接--跳转到Success 页面

    

猜你喜欢

转载自blog.csdn.net/jianghao233/article/details/81539319
今日推荐