springmvc環境

1、創建javaEE項目
2、導入相關jar包
鏈接:http://pan.baidu.com/s/1jIvGsKY 密碼:ewsc

3、編寫web.xml

        <!-- 配置dispatcherServlet -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置文件路徑 -->
            <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>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

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: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">
        <!-- 配置自動掃描的包 -->
        <context:component-scan base-package="com.xhs"></context:component-scan>
        <!-- 配置視圖解析器,如何把handler方法返回值解析爲實際物理視圖 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!-- 配置視圖 BeanNameViewResolver 解析器:使用視圖的名字來解析視圖 -->
        <!-- 通過order屬性來定義視圖解析器的優先級,order值越小,則優先級越高 -->
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
            <property name="order" value="100"></property>
        </bean>
        <!-- 配置國際化資源文件 -->
        <bean id="messageSource"
            class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="i18n"></property>
        </bean>
        <mvc:view-controller path="/success" view-name="success" />
        <!-- 在實際開發中通常都需配置 mvc:annotation-driven 標籤 -->
        <mvc:annotation-driven></mvc:annotation-driven>
    </beans>
5、在WEN-INF下新建pages文件夾
6、在自動掃描的包下新建類

    package com.xhs;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Controller
    @RequestMapping(value="/springmvc")
    public class springmvc1 {
        @RequestMapping("hello")
        public String hello() {
            System.out.println("進入此方法");
            return "success";
        }
    }
7、在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>
        環境搭建
        <br>
        <a href="springmvc/hello">點擊挑轉</a>
    </body>
    </html>
在pages文件夾新建success.xml

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 成功界面
  11. </body>
  12. </html>





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章