[英]How to Add a Controller in Spring MVC 3?
I have created a new Spring MVC 3 project using NetBean. 我使用NetBean创建了一个新的Spring MVC 3项目。 But there is no option of adding a new controller in the IDE.
但是没有选择在IDE中添加新控制器。
Well adding a Controller is as simple as adding a class annotated with 添加一个Controller就像添加一个带注释的类一样简单
@Controller
And specifying the package to be scanned from applicationContext.xml which in turn is specified in the web.xml. 并从applicationContext.xml指定要扫描的包,而applicationContext.xml又在web.xml中指定。 Something like this:
像这样的东西:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/applicationContext.xml
</param-value>
</context-param>
in web.xml 在web.xml中
Then in /WEB-INF/spring/appServlet/applicationContext.xml : 然后在/WEB-INF/spring/appServlet/applicationContext.xml中:
<context:component-scan base-package="your.package" />
Of course you need the actual schema in your applicationContext.xml 当然,您需要applicationContext.xml中的实际架构
xmlns:context="http://www.springframework.org/schema/context"
And under schema location: 在架构位置下:
http://www.springframework.org/schema/context/spring-context-3.0.xsd
And then a class : 然后是一堂课:
package your.package
.....
@Controller
MyController{
.....
If you are using an annotation driven implementation of Spring you don't need to do anything special. 如果您使用Spring的注释驱动实现,则无需执行任何特殊操作。 Create a standard Java class inside the package that Spring is configured to scan.
在Spring配置为扫描的包内创建标准Java类。 Then annotate the class with
@Controller
then create your method(s) and mappings using @RequestMapping
. 然后使用
@Controller
注释该类,然后使用@RequestMapping
创建方法和映射。
In its simplest form a controller would be something like: 在最简单的形式,控制器将是这样的:
@Controller
public class MyClass {
@RequestMapping("/myUrlMapping.do")
public ModelAndView myMethod() {
return new ModelAndView("myView");
}
}
This assumes you already have Spring configured correctly. 这假设您已经正确配置了Spring。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.