springmvc入门程序HelloWord

来源:互联网 发布:空燃比传感器数据分析 编辑:程序博客网 时间:2024/05/17 01:42

一、环境搭建:
1.创建web项目
2.导入jar
ioc:
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
springmvc:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
日志包
commons-logging-1.1.3.jar
aop:
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
3.配置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>springmvc02</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <!--配置Springmvc用来处理请求的前端控制器(DispatcherServlet)  -->  <!-- The front controller of this Spring Web application, responsible for handling all application requests    这个Spring Web应用程序的前端控制器(DispatcherServlet),负责处理所有应用程序请求   -->  <servlet>    <servlet-name>springDispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!--初始化参数         contextConfigLocation:指定Springmvc配置文件位置 -->    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:springmvc.xml</param-value>    </init-param>    <!-- 定义启动顺序:数值越小优先级越高,(越早创建 )-->    <load-on-startup>1</load-on-startup>  </servlet>  <!-- Map all requests to the DispatcherServlet for handling        将所有请求映射到DispatcherServlet处理     -->  <servlet-mapping>    <servlet-name>springDispatcherServlet</servlet-name>    <!--        /:也是处理所有请求,不会拦截所有*.jsp的请求        /*:也是处理所有请求        区别。/* 不仅是所有请求,连 *.jsp的请求都会由这个Servlet处理.        *.jsp的请求应该交给服务器自己处理        为了后面来支持rest风格的编程         -->    <url-pattern>/</url-pattern>  </servlet-mapping></web-app>

4.在src下创建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:aop="http://www.springframework.org/schema/aop"    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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <!--1、配置包扫描 -->     <context:component-scan base-package="controller"></context:component-scan>    <!-- 配置视图解析器,负责地址 -->    <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        /WEB-INF/page+return值+.jsp        在controller中return文件名称就ok        前缀 :jsp所在的目录        <property name="prefix" value="/WEB-INF/page"></property>        后缀 :文件的扩展名        <property name="suffix" value=".jsp"></property>    </bean> --></beans>

5.创建HelloController文件放在controller包下

package controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * @Controller 告诉springmvc这是一个处理请求的处理器 * @author lenovo *@RequestMapping告诉springmvc处理那个请求 */@Controllerpublic class Hello {    //http://localhost:8888/SpringMVC/index.jsp    @RequestMapping("/show")    public void show(){        System.out.println("hello springmvc!");        return "success.jsp";    }}

6.创建index.jsp页面

<a href="/show"></a>

7.创建success.jsp页面

<body>    This is my JSP page. <br>  </body>

8.访问地址
http://localhost:8888/项目名称/index.jsp