Java 系列之spring学习--springmvc注解方式(五)

一、springmvc注解方式

  注解方式使用的更多,更加灵活。在上一篇的博客的基础上修改springmvc-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: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-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	<!-- <bean name="/hello" class="springmvc.HelloController"></bean> -->
	<!-- 要使注解生效,需要配置这几个属性,它会扫描controller中的注解 -->
    <context:component-scan base-package="Controller" />
    <mvc:default-servlet-handler />
    <mvc:annotation-driven />
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 <property name="prefix" value="/WEB-INF/JSP/"/>
        <property name="suffix" value=".jsp"/>
        </bean>
</beans>

二、在controller下新建一个类

package Controller;

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

@Controller
public class UserController {

	@RequestMapping(value="/index")
	public ModelAndView index()
	{
		System.out.println("index");
		ModelAndView mv=new ModelAndView();
		mv.setViewName("hello");
		return mv;

	}
}

三、访问/index即可

原文地址:https://www.cnblogs.com/WJ--NET/p/8329628.html

时间: 2024-08-24 18:08:00

Java 系列之spring学习--springmvc注解方式(五)的相关文章

Java 系列之spring学习--springmvc注解参数传递(六)

一.绑定参数注解如下 @RequestParam     绑定单个请求数据,既可以是URL中的参数,也可以是表单提交的参数或上传的文件. 它有三个属性:  value    用于设置参数名. defaultValue    用于对参数设置默认值.         required    用于设置是否必需值,默认为true.为true时,如果参数为空,会报错. @PathVariable    绑定URL中的参数值 它只有一个属性值value 访问地址:http://localhost:8080/

Java 系列之spring学习--注解(三)

一.注解 使用注解之前要开启自动扫描功能 <?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:context="http://www.sp

Java 系列之spring学习--spring搭建(一)

一.新建maven项目 二.引入spring jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Respository:标识持久层组件 3.@Service:标识业务层组件 4.@Controller:标识表现层组件 Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称 当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件

springMVC注解方式+easyUI+MYSQL配置实例

刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+easyUI+MYSQL的方式构建项目,所以要下载spring的jar包.easyUI资源.mysql包. 2.新建空项目名称为test,将架包导入项目.即把下载来的spring-framework-3.1.1.RELEASE/libs中的对应jar包复制到项目的/WebRoot/WEB-INF/li

2.学习SpringMVC注解入门篇

一.SpringMVC执行流程 . 二.创建项目学习SpringMVC注解 按照我之前的SpringMVC创建项目,首先创建一个项目springmvc01,配置好pom.xml,web.xml,springmvc.xml文件之后创建一个HelloController. 1.RequestMapping注解 1 //控制器类 2 @Controller 3 @RequestMapping("/user") 4 public class HelloController { 5 6 @Req

springmvc3.2+spring+hibernate4全注解方式整合(一)

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.

spring AOP自定义注解方式实现日志管理

转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <conte

一个简单的Spring定时器例子 注解方式

首先在applicationContext.xml中增加 文件头中增加一条 xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation 中增加一条 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd <beans xmlns:task=&quo