![](/img/trans.png)
[英]How to add a custom security annotation to Spring MVC controller method
[英]How to Add a Controller in Spring MVC 3?
我使用NetBean创建了一个新的Spring MVC 3项目。 但是没有选择在IDE中添加新控制器。
添加一个Controller就像添加一个带注释的类一样简单
@Controller
并从applicationContext.xml指定要扫描的包,而applicationContext.xml又在web.xml中指定。 像这样的东西:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/applicationContext.xml
</param-value>
</context-param>
在web.xml中
然后在/WEB-INF/spring/appServlet/applicationContext.xml中:
<context:component-scan base-package="your.package" />
当然,您需要applicationContext.xml中的实际架构
xmlns:context="http://www.springframework.org/schema/context"
在架构位置下:
http://www.springframework.org/schema/context/spring-context-3.0.xsd
然后是一堂课:
package your.package
.....
@Controller
MyController{
.....
如果您使用Spring的注释驱动实现,则无需执行任何特殊操作。 在Spring配置为扫描的包内创建标准Java类。 然后使用@Controller
注释该类,然后使用@RequestMapping
创建方法和映射。
在最简单的形式,控制器将是这样的:
@Controller
public class MyClass {
@RequestMapping("/myUrlMapping.do")
public ModelAndView myMethod() {
return new ModelAndView("myView");
}
}
这假设您已经正确配置了Spring。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.