简体   繁体   English

在App Engine中使用Spring

[英]Using Spring with App Engine

I am struggeling using the basic crud methods with app engine: 我正在努力通过App Engine使用基本的Crud方法:

My code looks like that: 我的代码如下所示:

My CustomerController: 我的CustomerController:

@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";

}

my mvc-dispatcher-servlet: 我的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>

and the form I want to proceede in command.jsp: 以及要在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>

When I want to proceede the form from http://127.0.0.1:8888/command.jsp . 当我想从http://127.0.0.1:8888/command.jsp进行表格处理时。 I get: 我得到:

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

In GAE I get: 在GAE中,我得到:

WARNING: No mapping found for HTTP request with URI [/add] in DispatcherServlet with name 'mvc-dispatcher' 警告:在名称为'mvc-dispatcher'的DispatcherServlet中,找不到带有URI [/ add]的HTTP请求的映射

Please help me to find what is wrongly mapped in my application. 请帮助我找到我的应用程序中错误映射的内容。

UPDATE 更新

<?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>

UPDATE - 2 更新-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

Now i am getting a different exception. 现在我得到了一个不同的例外。 However, I changed nothing from before;P 但是,我从没改变过; P

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

Can you also add the server log when you startup your AppEngine app in your IDE please? 在IDE中启动AppEngine应用时,是否还可以添加服务器日志? Spring logs all the URL mappings on startup. Spring在启动时记录所有URL映射。

Also in your context:component-scan config, your base package has an uppercase character in it ? 同样在您的context:component-scan配置中,您的基本包中是否有大写字符?

com.Trium.controller

I'm trying to guess here since I don't know CustomerController's package. 我想在这里猜测,因为我不知道CustomerController的程序包。

I feel you are not returning the login page when localhost is actually called. 我觉得当实际调用localhost时,您没有返回登录页面。 You need to add the following controller mapping to load homepage. 您需要添加以下控制器映射以加载主页。

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

Now access your localhost:8888 as your login page. 现在,作为登录页面访问您的localhost:8888。

Try returning the name of the page instead of returning the URL. 尝试返回页面名称而不是返回URL。

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

You have this: 你有这个:

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

which handles GET requests. 处理GET请求。 But your command.jsp form does a POST to add 但是您的command.jsp表单会add一个POST

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

So you need another handler method to handle that action. 因此,您需要另一个处理程序方法来处理该操作。 It appears to me you already have that in the other handler method 在我看来,您已经在其他处理程序方法中拥有该功能

@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");

}

Change your form to POST to /list 将您的表单更改为POST/list

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

Also, you shouldn't do redirect:command . 另外,您不应该执行redirect:command That basically makes a new request to localhost:8888/command which doesn't exist. 这基本上向localhost:8888/command发出了一个新请求,该请求不存在。 Do it to redirect:add which will call the handler method above and render command.jsp . 进行redirect:add ,它将调用上面的处理程序方法并呈现command.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM