Spring MVC(半注解模式)

基于半注解方式的项目配置

一、创建maven web项目

1.1 创建项目,选择war打包形式,自动补全包配置

1.2 设置项目的编码格式为utf-8

1.3 设置项目的编译版本为JDK1.8

 

1.4 设置项目的服务器为Tomcat

 二、为项目添加依赖

2.1 配置pom.xml文件,添加spring mvc依赖

<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.3.9.RELEASE</version>
	</dependency>
</dependencies>

2.2 在项目的resource的目录中添加核心配置文件spring-configs.xml,并进行基础的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
       
</beans>

三、配置组件扫描与视图解析

在spring mvc 核心配置文件中功能组件

<!--配置组件扫描 -->
<context:component-scan base-package="com.jk" />
<!-- 启用MVC默认配置 (@RequestMapping) -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean id="viewResolver"
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/pages/" />
	<property name="suffix" value=".jsp" />
</bean>

四、配置前端控制器

在web.xml文件中配置DispatcherServlet对象

<?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_2_5.xsd" version="2.5">
  <display-name>Spring-MVC-02</display-name>
  <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:spring-configs.xml</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
 	  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

 五、创建后端控制器

在指定的包中创建控制层对象

package com.jk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/req/")
public class AnnotationHelloController{

	@RequestMapping("doSayHello")
	public ModelAndView doSayHello() {
		ModelAndView mv=new ModelAndView("hello");
		mv.addObject("message", "helloworld");
		return mv;
	}
@RequestMapping("doSayWelcome")
	public String doSayWelcome() {
      	return "hello";
	}
@RequestMapping("doHandleRequest")
	public String doHandleRequest(Model m) {
         m.addAttribute("message","welcome to beijing");
      	return "hello";
	}
}

六、创建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>
	<h1>welcome to here...</h1>
	<h1>${ message }</h1>
</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值