[英]How can I create a Spring controller without the use of the annotations?
I am studying for the Spring Core certification and I have some doubts related this question: 我正在攻读Spring Core认证,我对这个问题有些怀疑:
What is the @Controller annotation used for? @Controller注释用于什么? How can you create a controller without an annotation? 如何在没有注释的情况下创建控制器?
So I know that the @Controller annotation indicates that a particular class serves the role of a controller. 所以我知道@Controller注释表明一个特定的类服务于控制器的角色。 The @Controller annotation acts as a stereotype for the annotated class, indicating its role. @Controller注释充当带注释的类的构造型,指示其角色。 The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations. 调度程序扫描这些带注释的类以查找映射方法,并检测@RequestMapping注释。
So a controller class is something like this: 所以控制器类是这样的:
@Controller
public class AccountController {
@RequestMapping("/listAccounts")
public String list(Model model) {...}
}
}
Ok, this is pretty clear for me but what exactly means create a controller without an annotation? 好的,这对我来说非常清楚,但究竟是什么意思创建一个没有注释的控制器? How can I do it? 我该怎么做? By XML configuration or how? 通过XML配置还是如何?
Tnx TNX
public class AccountController extends MultiActionController {
public ModelAndView listAccounts() {
// your code
return new ModelAndView("listAccounts.bean", "msg", "As you want")
}
}
web.xml web.xml中
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.bean</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml 调度员servlet.xml中
<bean name="/listAccounts.bean" class="p1.AccountController"></bean>
I've come across this: Spring MVC 3.1 without annotations? 我遇到过这个问题: 没有注释的Spring MVC 3.1?
It would seem that you can actually create a controller without annotations (I've been working with Spring for little over a year and haven't encountered such a scenario, I don't know why one would want to do this, apart from certification questions of course) and the way to do it is by using a special configuration in the dispatcher-servlet XML file. 看起来你实际上可以创建一个没有注释的控制器(我已经和Spring一起工作了一年多而没有遇到过这种情况,我不知道为什么人们会想要这样做,除了认证当然是问题)以及执行此操作的方法是在dispatcher-servlet XML文件中使用特殊配置。
只是为了评论为什么有人想以编程方式或通过XML来配置Spring的原因,这是因为在运行时需要扫描所有寻找注释的文件,因此如果我们禁用扫描并手动配置它,应用程序将可用更快地服务请求,这对于高需求的场景特征非常重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.