在类上与方法上添加相应注解即可。
@Controller:表示当前类为处理器,创建处理器对象。@RequestMapping:表示当前方法为处理器方法。该方法要对 value 属性所指定的 URI进行处理与响应。被注解的方法的方法名可以随意。

package com.wzy.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class MyController {/*** RequestMapping 请求映射,把指定的请求交给方法处理* 当启动服务器时候,访问的是 / 也就是上下文路径,接收并执行方法* / 表示 http://localhost:8080/springmvc/* 返回 http://localhost:8080/springmvc/ + 视图解析器前缀 WEB-INF/templates/ + index + 后缀.html* @return*/@RequestMapping(value = "/")public String index() {return "index";//返回视图名称}@RequestMapping(value = "/target")public String toTarget() {return "target";}}
