SpringMVC学习(一)-HelloWorld

来源:互联网 发布:中国数据网 编辑:程序博客网 时间:2024/05/16 10:42

SpringMVC实现的步骤

1、加入相关jar包,在maven项目里就是在pom.xml文件中引入相关实际依赖

2、在web.xml文件中配置DispatcherServlet

3、加入springmvc配置文件

4、编写处理请求的请求处理器,并标识为处理器

5、编写视图

1.web.xml

<?xml version="1.0" encoding="utf-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <!-- 配置DispatcherServlet -->    <!-- 配置DispatcherServlet的作用是:如果在某个方法上配置了@RequestMapping("/helloworld"),         当浏览器访问helloworld时,DispatcherServlet会将这个请求发送给@RequestMapping("/helloworld")         所在的方法上,执行这个方法 -->    <servlet>        <servlet-name>SpringDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置DispatcherServlet的一个初始化参数:配置springMVC配置文件的位置和名称 -->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param>    <!--SpringDispatcherServlet在当前web应用被加载的时候被创建,而不是等第一次请求的时候被创建  -->        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>SpringDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>  <!-- 可以应答所有请求 -->    </servlet-mapping></web-app>

2.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/beans                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd                           http://www.springframework.org/schema/context                            http://www.springframework.org/schema/context/spring-context-3.2.xsd                           http://www.springframework.org/schema/mvc                            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">    <!-- 配置自动扫描的包 -->    <context:component-scan base-package="com.springMVC"></context:component-scan>    <!-- 配置视图解析器:如何把handler方法返回值解析为实际视图 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

3.java类

package com.springMVC.Helloworld;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class Helloworld {    /*     * 1.使用@RequestMapping注解来映射请求的URL     * 2.返回值会通过视图解析器为实际的物理视图,对于InternalResourceViewResolver     *通过prefix+returnVal+后缀 这样的方式得到实际的物理视图,然后做转发操作     * /WEB-INF/views/success.jsp    */    @RequestMapping("/helloworld")    public String  hello() {        System.out.println("hello world");        return "success";    }}

4.简单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>    <a href="helloworld">Hello World</a></body></html>
<%@ 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>    <h2>success page</h2></body></html>
原创粉丝点击