springMVC(一)——servlet.xml配置

一.创建HelloWeb的Dynamic Web项目
二.在新建的项目src下创建com.HelloWeb.controller的包
三.将要用到的spring jar包放在WebRoot/WEB-INF/lib下
四.在com.HelloWeb.controller包下创建名为:HelloController.java的类

/**
 * @author wuchao
 * @time 下午4:29:01
 * @description TODO
 */
package com.HelloWeb.controller;

import java.lang.reflect.Method;

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

/**
 * @author wuchao
 * @time 下午4:29:01
 *
 */
@Controller
@RequestMapping("/HelloWeb")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String pringHello(ModelMap model){
        model.addAttribute("massage", "Hello,spring MVC Framework");
        return "HelloWeb";
    }
}

@Controller
引用DispatcherServlet的Controller,接受用户请求,并根据GET或者POST方法调用相应的服务方法,并将视图名称返回给DispatcherServlet
@RequestMapping
经过测试 RequestMapping在这个项目中并没有起到作用 真正起到作用的是return”HelloWeb”
五.WebContent/WEB-INF文件夹下创建SpringMVC配置文件web.xml和HelloWeb-servlet.xml
web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- 服务器显示名字 -->
    <display-name>Spring MVC Application</display-name>
    <!-- 加载servlet转发器 -->
    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- servlet映射 主要是为了页面访问根目录 -->
    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

HelloWeb-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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">

   <!-- 扫描包 -->
   <context:component-scan base-package="com.HelloWeb"></context:component-scan>
   <!-- 配置驶视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/jsp/"></property>
   <property name="suffix" value=".jsp"></property>
   </bean>
   </beans>

六.在WebContent/WEB-INF文件夹下创建一个名为jsp的子文件夹。在此子文件夹下创建视图文件HelloWeb.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>springMVC第一个网页</title>
</head>
<body>
<h1>hello,${massage}</h1>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值