SpringMVC(二)SpringMVC入门程序

SpringMVC学习笔记(二)

一、创建web项目并导入相应jar包

在这里插入图片描述

在这里插入图片描述

二、创建是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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation=" http://www.springframework.org/schema/mvc 
    					http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    					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.2.xsd">
        				
        	       <!-- 配置自动扫描的包 -->
        <mvc:annotation-driven/>
        <context:component-scan base-package="springMVC3.controller"></context:component-scan>
 
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>        		        				
</beans>

三、在WebContext/WEB-INF文件下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_2_5.xsd"
    id="WebApp_ID" version="2.5"> 
    <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置Spring mvc下的配置文件的位置和名称 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classPath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

到这里,我们的环境配置已经弄好了!

四、创建请求页面和相应页面

请求页面:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <a href="g/h">测试超链接1:点击测试能否跳转</a>
</body>
</html>

相应页面: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=ISO-8859-1">
<title>成功界面</title>
</head>
<body>
 
<h4>这是第一个SpringMVC程序</h4>
 
</body>
</html>

五、在创建controller

package springMVC3.controller;

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

@Controller
@RequestMapping("/g")//等于@RequestMapping(value="/g")
public class helloworldController {
    
    
	 @RequestMapping(value="/h")   
	    public String hello() {
    
     
	    	System.out.println("hello world");       
	        return "success";
	    }
}

 运行index.jsp:
 查看结果
在这里插入图片描述
运行成功!

猜你喜欢

转载自blog.csdn.net/qq_46046423/article/details/114654051