如何在不使用注释的情况下创建 Spring 控制器? [英] How can I create a Spring controller without the use of the annotations?
问题描述
我正在学习 Spring Core 认证,我对这个问题有一些疑问:
I am studying for the Spring Core certification and I have some doubts related this question:
@Controller 注解是干什么用的?你怎么能创建一个没有注释的控制器?
What is the @Controller annotation used for? How can you create a controller without an annotation?
所以我知道 @Controller 注释表明一个特定的类充当控制器的角色.@Controller 注释充当被注释类的构造型,表明它的作用.调度程序扫描此类带注释的类以查找映射方法并检测 @RequestMapping 注释.
So I know that the @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.
所以控制器类是这样的:
So a controller class is something like this:
@Controller
public class AccountController {
@RequestMapping("/listAccounts")
public String list(Model model) {...}
}
}
好的,这对我来说很清楚,但是创建一个没有注释的控制器究竟意味着什么?我该怎么做?通过XML配置还是如何?
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?
Tnx
推荐答案
public class AccountController extends MultiActionController {
public ModelAndView listAccounts() {
// your code
return new ModelAndView("listAccounts.bean", "msg", "As you want")
}
}
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
<bean name="/listAccounts.bean" class="p1.AccountController"></bean>
这篇关于如何在不使用注释的情况下创建 Spring 控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!