欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring MVC(半注解模式)

程序员文章站 2024-02-26 13:06:28
...

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

一、创建maven web项目

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

Spring MVC(半注解模式)

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

Spring MVC(半注解模式)

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

Spring MVC(半注解模式)

Spring MVC(半注解模式)

 

1.4 设置项目的服务器为Tomcat

Spring MVC(半注解模式)

 二、为项目添加依赖

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>