SpringMVC首个程序

参考:

  • http://www.yiibai.com/spring_mvc/springmvc_hello_world_example.html
  • http://www.jianshu.com/p/0ccaa4af05fc





    其中本来是打算按照易百教程一口气弄完,不过不知道为啥自己的电脑上一直出错,可能是环境不一样吧。然后百度了很久才找到一个可使用的简单的初始教程。因为是首个程序,里面大部分都不怎么了解,只能按部就班的跟随制作

    我是使用maven来辅助制作的,其中大部分包和易百教程所用的包的版本差不多,不过最好还是使用最新的。
    另外有一点较重要的是spring-core要使用高版本一点的包,不然会报错,下面是pom.xml中的依赖内容(我是由于电脑上有4.1.4的包,所以可以的话是直接使用那部分的,若是新建的话,建议将所有的包换成最新的,可以在http://search.maven.org/#browse进行搜索)

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- spring-benas -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <!-- spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <!-- spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

一样是使用eclipse+maven两者集合的注意勾选project facet中的
这里写图片描述
不是的只要创建web项目是有勾选tomcat的也可以
下面是web.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>项目名</display-name>
    <!-- 配置SpringMVC分发器,拦截所有请求 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>namespace</param-name>
            <param-value>dispatcher-servlet</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
同时根据上面的内容来创建dispatcher-servlet.xml(其他名字也可以,不过要根据web.xml的内容,默认是为项目名-servlet.xml,默认和web.xml放在一起,想要更改路径则在value那一栏上进行设置)

下面是dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    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.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
      ">
         <!-- 开启注解模式驱动 -->    
         <mvc:annotation-driven />
         <!-- 扫包 -->
         <context:component-scan base-package="com.springmvc.*" />
         <!-- 视图渲染 jsp/freemaker/velocity-->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <!-- 制定页面存放的路径 -->
               <property name="prefix" value="/"></property>
               <!-- 文件的后缀 -->
               <property name="suffix" value=".jsp"></property>
         </bean> 
</beans>

同时在同一目录下需放置applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
    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/tx   
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/context   
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/util   
  http://www.springframework.org/schema/util/spring-util-4.0.xsd
  ">
</beans>

另外由上面的dispatcher-servlet中可以的设置,需要将servlet(controller类)放置在com.springmvc下面的包中
以下是放置再com.springmvc.controller下面示例文件

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }

}

下面是配合HelloController使用的hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>Hello, ${message}</h2>
</body>
</html>

运行结构为
这里写图片描述
首个springmvc程序就这样运行结束了,不过感觉若是自己会看去看源代码的文档,可能其中又有部分的配置可以进行省略掉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值