我是Spring框架的新手。我刚开始在netbeans中实现多动作控制器。但是。我得到了上面的错误。我把代码粘贴在下面。请调查一下并解决这个问题。
dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="/*.htm" class="controller.MyController"/>
</beans>
index . jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h4>Multi Action Controller Example</h4>
<a href="add.htm">Add</a>
<a href="update.htm">Update</a>
<a href="remove.htm">Remove</a>
</body>
</html>
MyController.java:
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class MyController extends MultiActionController {
public ModelAndView add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("Add ma");
return new ModelAndView("result","message","Add Method Called");
}
public ModelAndView update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("Update ma");
return new ModelAndView("result","message","Update Method Called");
}
public ModelAndView remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("Remove ma");
return new ModelAndView("result","message","Remove Method Called");
}
}
result.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nee Varaatha</title>
</head>
<body>
<h1>Please Show it </h1>
${message}
</body>
</html>
尝试在netbeans
的输出窗口中检查您的apachelog,看看其中的控制器默认映射是什么,并将该映射添加到index.jsp
中。
例如在日志中你会发现
将URL路径[/employee]映射到处理程序'/*.html'
只需在JSP
你考虑过使用@Controller注释类吗?由于spring 2.5注释通常是首选。
下面是一个示例web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<display-name>Example</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/example-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
example-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--Tell the servlet where to look for annotated methods-->
<context:component-scan base-package="controller" />
<!--if no controller logic is required, mvc:view-controller can be used to simply show a view for a request -->
<mvc:view-controller path="/" view-name="index"/>
<!--Enables many annotations and searches for @Controller annotated methods etc.. -->
<context:annotation-config />
<!--JSR-303 (Bean validation) support will be detected on classpath and enabled automatically-->
<mvc:annotation-driven />
<!--This tag allows for mapping the DispatcherServlet to "/" (all extensions etc)-->
<mvc:default-servlet-handler/>
<mvc:resources location="/resources/**, classpath:resources" mapping="/resources/**"/>
<!--Configures the application to search for views in folder /WEB-INF/jsp/ with the suffix ".jsp"
in controllers prefix and suffix are therefore no longer needed-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
和一个简单的控制器示例:
@Controller
public class ExampleController {
@RequestMapping("/test")
public String test(Model model) {
model.addAttribute("message","Test message");
return "result";
}
}
也可参阅此处的spring参考文档