[英]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.