springmvc中一个Action中,写多个类似的业务控制方法+在业务控制方法中写入普通变量收集参数+限定某个业务控制方法。只允许GET或POST请求方式访问

时间:2022-03-17 20:03:31

springmvc中一个Action中,写多个类似的业务控制方法+在业务控制方法中写入普通变量收集参数+限定某个业务控制方法,只允许GET或POST请求方式访问

在UserAction中

@Controller
@RequestMapping(value="/user")
public class UserAction {
/**
* 用户注册,只能接收POST请求
*/
@RequestMapping(method=RequestMethod.POST,value="/register")
public String registerMethod(Model model,String username,String salary) throws Exception{
System.out.println("用户注册-->" + username + ":" + salary);
model.addAttribute("message","员工注册成功");
return "/jsp/success.jsp";
}
/**
* 用户登录,即能接收POST请求,又能接收GET请求
*/
@RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET})
public String loginMethod(Model model,String username) throws Exception{

System.out.println("用户登录-->" + username);
model.addAttribute("message","员工登录成功");
return "/jsp/success.jsp";
}
}

在spring.xml中

<!-- Action,让springioc容器去扫描带@Controller的类 -->
<context:component-scan base-package="cn.itcast.javaee.springmvc.app16"/>
<!-- 映射器(省) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>