0

我正在努力使用带有应用程序引擎的基本 crud 方法:

我的代码如下所示:

我的客户控制器:

@Controller
public class CustomerController {

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAddCustomerPage(ModelMap model) {
   return "command";
}

@RequestMapping(value = "/list", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request, ModelMap model) {

    String email = request.getParameter("email");
    String password = request.getParameter("password");

    Customer c = new Customer();
    c.setEmail(email);
    c.setPassword(password);
    c.setDate(new Date());

    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        pm.makePersistent(c);
    } finally {
        pm.close();
    }

    return new ModelAndView("redirect:command");

}

// get all customers
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String listCustomer(ModelMap model) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query q = pm.newQuery(Customer.class);
    q.setOrdering("date desc");

    List<Customer> results = null;

    try {
        results = (List<Customer>) q.execute();

        if (results.isEmpty()) {
            model.addAttribute("customerList", null);
        } else {
            model.addAttribute("customerList", results);
        }

    } finally {
        q.closeAll();
        pm.close();
    }

    return "list";

}

我的 mvc-dispatcher-servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">

<context:component-scan base-package="com.Trium.controller" />
<mvc:annotation-driven />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

以及我想在 command.jsp 中处理的表单:

<form id="myForm" class="form-horizontal" action="add"
                                    method="post">
                                    <fieldset>
                                        <div class="control-group">

                                            <!-- Text input-->
                                            <label class="control-label" for="input01">Email:</label>
                                            <div class="controls">
                                                <input name="email" placeholder="email"
                                                    class="input-xlarge" type="text"
                                                    value="<%=request.getParameter("email")%>">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <!-- Text input-->
                                            <label class="control-label" for="input01">Password:</label>
                                            <div class="controls">
                                                <input name="password" placeholder="password"
                                                    class="input-xlarge" type="text"
                                                    value="<%=request.getParameter("password")%>">
                                            </div>
                                        </div>

                                    </fieldset>
                                </form>

当我想从http://127.0.0.1:8888/command.jsp. 我得到:

http://127.0.0.1:8888/add 错误 404

在 GAE 我得到:

警告:在名称为“mvc-dispatcher”的 DispatcherServlet 中未找到带有 URI [/add] 的 HTTP 请求的映射

请帮助我找出我的应用程序中错误映射的内容。

更新

<?xml version="1.0" encoding="UTF-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Servlets -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Default page to serve -->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>SystemServiceServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value/>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>SystemServiceServlet</servlet-name>
        <url-pattern>/_ah/spi/*</url-pattern>
    </servlet-mapping>

</web-app>

更新 - 2

    Mär 26, 2013 4:59:11 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
INFO: javax.servlet.ServletContext log: Initializing Spring root WebApplicationContext
Mär 26, 2013 4:59:11 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Mär 26, 2013 4:59:17 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Mar 26 16:59:17 UTC 2013]; root of context hierarchy
Mär 26, 2013 4:59:19 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
Mär 26, 2013 4:59:21 PM org.springframework.context.annotation.ClassPathBeanDefinitionScanner registerDefaultFilters
INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
Mär 26, 2013 4:59:25 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Mär 26, 2013 4:59:25 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7f5fde46: defining beans [customerController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/list],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.add(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/delete/{key}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.delete(java.lang.String,javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/update],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.update(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/add],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.Trium.controller.CustomerController.getAddCustomerPage(org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/update/{name}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.Trium.controller.CustomerController.getUpdateCustomerPage(java.lang.String,javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/list],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.Trium.controller.CustomerController.listCustomer(org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/login],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.login(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:30 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 18804 ms
Mär 26, 2013 4:59:37 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
INFO: javax.servlet.ServletContext log: Initializing Spring FrameworkServlet 'mvc-dispatcher'
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'mvc-dispatcher': initialization started
Mär 26, 2013 4:59:37 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Tue Mar 26 16:59:37 UTC 2013]; parent: Root WebApplicationContext
Mär 26, 2013 4:59:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
Mär 26, 2013 4:59:37 PM org.springframework.context.annotation.ClassPathBeanDefinitionScanner registerDefaultFilters
INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
Mär 26, 2013 4:59:37 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Mär 26, 2013 4:59:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3c050a95: defining beans [customerController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@7f5fde46
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/list],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.add(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/delete/{key}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.delete(java.lang.String,javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/update],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.update(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/add],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.Trium.controller.CustomerController.getAddCustomerPage(org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/update/{name}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.Trium.controller.CustomerController.getUpdateCustomerPage(java.lang.String,javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/list],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.Trium.controller.CustomerController.listCustomer(org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:37 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/login],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.Trium.controller.CustomerController.login(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)
Mär 26, 2013 4:59:38 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'mvc-dispatcher': initialization completed in 657 ms
Mär 26, 2013 4:59:38 PM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Started SelectChannelConnector@0.0.0.0:8888
Mär 26, 2013 4:59:38 PM com.google.appengine.tools.development.AbstractServer startup
INFO: Server default is running at http://localhost:8888/
Mär 26, 2013 4:59:38 PM com.google.appengine.tools.development.AbstractServer startup
INFO: The admin console is running at http://localhost:8888/_ah/admin
Mär 26, 2013 5:59:38 PM com.google.appengine.tools.development.DevAppServerImpl start
INFO: Dev App Server is now running
Mär 26, 2013 6:00:11 PM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported

现在我得到了一个不同的例外。但是,我和以前没有任何改变;P

**WARNING: Request method 'POST' not supported**
4

3 回答 3

2

当您在 IDE 中启动 AppEngine 应用程序时,您还可以添加服务器日志吗?Spring 在启动时记录所有 URL 映射。

同样在您的上下文:组件扫描配置中,您的基本包中有一个大写字符?

com.Trium.controller

我想在这里猜测,因为我不知道 CustomerController 的包。

于 2013-03-26T16:41:50.643 回答
2

你有这个:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAddCustomerPage(ModelMap model) {
   return "command";
}

处理GET请求。但是你的 command.jsp 表单做了POST一个add

<form id="myForm" class="form-horizontal" action="add" method="post">

因此,您需要另一个处理程序方法来处理该操作。在我看来,您已经在其他处理程序方法中拥有它

@RequestMapping(value = "/list", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request, ModelMap model) {

    String email = request.getParameter("email");
    String password = request.getParameter("password");

    Customer c = new Customer();
    c.setEmail(email);
    c.setPassword(password);
    c.setDate(new Date());

    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        pm.makePersistent(c);
    } finally {
        pm.close();
    }

    return new ModelAndView("redirect:command");

}

将您的表格更改POST/list

<form id="myForm" class="form-horizontal" action="list" method="post">

另外,你不应该这样做redirect:commandlocalhost:8888/command这基本上提出了一个不存在的新请求。这样做redirect:add将调用上面的处理程序方法并渲染command.jsp

于 2013-03-26T19:46:11.387 回答
2

我觉得实际调用 localhost 时您没有返回登录页面。您需要添加以下控制器映射来加载主页。

@RequestMapping(value = "/", method = RequestMethod.GET)
public String indexPage(ModelMap map) {
   return "login";
}

现在访问您的 localhost:8888 作为您的登录页面。

尝试返回页面名称而不是返回 URL。

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAddCustomerPage(ModelMap model) {
  return "command";
}
于 2013-03-26T19:05:16.460 回答