月光代碼園

記錄、分享、交流

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

SpringMVC配置入門

我的開發環境
  開發工具:    springsource-tool-suite-2.9.0
  JDK版本:    1.6.0_29
  tomcat版本:apache-tomcat-7.0.26

  本文地址:http://www.cnblogs.com/sunang/p/3419544.html 轉載請注明出處^_^

本文要注意的點已经用          標注,請大家要特別注意。

go!

step1.在你的開發環境新建一個工程,引入jar包

Maven代碼:

复制代码
<!-- Spring MVC -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>
<!-- JSP -->
<dependency>    
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</dependency>
复制代码

 step2:編輯web.xml

复制代码
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- 配置SpringMVC前端Servlet控制器DispatcherServlet -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <!-- 配置初始的DispatcherServlet上下文配置文件加載路徑 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:conf/spring/spring-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 讓DispatcherServlet處理所有以".htm"結尾的URL -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <!-- 配置DispatcherServlet上下文配置文件加載路徑 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:conf/spring/spring-servlet.xml
        </param-value>
    </context-param>
    <!-- 配置上下文載入器 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>
复制代码

step3.編輯DispatcherServlet上下文配置文件

    在step2中,我們配置了DispatcherServlet上下文配置文件的路徑為conf/spring/spring-servlet.xml,所以在src/main/resources/conf/spring目錄下新建spring-servlet.xml文件,代碼如下:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置SpringMVC配置文件所需的xml文件解析模板 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
    xmlns:tx="http://www.springframework.org/schema/tx" 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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 配置控制器要掃描的包路徑 -->
    <context:component-scan base-package="www.asuan.com" />
    <!-- 配置視圖解析器 -->
    <bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
        <!-- 解析器解析/WEB-INF/jsp/路徑下,以.jsp結尾的視圖文件 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
         <property name="suffix" value=".jsp" />
    </bean>    
</beans>
复制代码

step4.編寫controller和jsp文件

  在step3中,我們配置了包掃描路徑為"www.asuan.com",所以在src/main/java目錄下新建包www.asuan.com.controller,在包內新建HelloWorldController.java,代碼如下:

复制代码
package www.asuan.com.controller;

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

@Controller
public class HelloWorldController {
    @RequestMapping("/helloWorld")//url字段名
    public String helloWorld() {
        return "helloWorld";//jsp文件名
    }
}
复制代码

    視圖解析路徑設置為WEB-INF/jsp,所以在WEB-INF/jsp目錄下新建helloWorld.jsp文件(文件名為controller方法中返回的字符串),代碼如下:

复制代码
<!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>
<h2>Hello World!</h2>
</body>
</html>
复制代码

step5.運行與調試
將工程部署到tomcat并運行,在瀏覽器輸入:http://localhost:8080/你設置的工程名/helloWorld.htm

運行結果:

complete!

 

 

posted on   bangdikka  阅读(2391)  评论(10编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示