如何在不使用注释的情况下创建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屋!