要让一个 java 类成为 Contrller 则只需要在类上加上 @Controller 注解,此类需要在 springmvc.xml 文件中 springmvc 自定义扫描包下。
在此类中即可以写方法来处理请求。
请求方法需要 @RequestMapping 注解。
1. 关于 RequestMapping 注解,常用的有两个参数,value 和 method。不指定默认为 value 参数。
1)value 的值为 请求 URI such as:/test
2)method 的值为 要处理的什么方法的请求。 such as : RequestMethod.GET
example:
the jsp as below
<form action="test" method="post">
UserName:<input type="text" name="userName"/><br>
Password:<input type="password" name="password"/><br>
<input type="submit" value="Submit" />
</form>
the class as below
@Controller
public
class
Handler{
//处理请求与方法名字无关 只与注解有关。
@RequestMapping
(
"/test"
)
public
String test(
){
return
"success"
;
}