1

我尝试从我的控制器获取模型元素到前端。我正在使用非常简单的对象。se.no.controllers.UserController

@Controller
public class UserController {

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public User register(@RequestBody User user) {
        System.out.println("Register!!");
        return user;
    }
...

阿贾克斯前锋

  var user = {
          name :  $('#username').val(),
          password :  $('#password').val(),
  };


  jQuery.ajax({
      url : context.get('root') + '/register',
      type : "POST",
      contentType: 'application/json',
      dataType: 'json',
      data : JSON.stringify(user),        
      success : function(resp) {   ...

web.xml

<display-name>WebApp</display-name>
<welcome-file-list>
    <welcome-file>/index</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>


<error-page>
    <error-code>404</error-code>
    <location>/error404</location>
</error-page>

和 spring-servlet.xml

<context:annotation-config />
<context:component-scan base-package="se.no.controllers,se.no.controllers,se.no.service,se.no.dao" /> 

<!-- also add the following beans to get rid of some exceptions -->
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>

<bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>
</bean>

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean> 

<tx:annotation-driven />
<mvc:default-servlet-handler/>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- the mvc resources tag does the magic -->
<mvc:resources mapping="/resources/**" location="/resources/" />

你能告诉我我错过了什么吗?我得到的结果是:

404 未找到 - localhost:8080/sklepi/register"

我在运行应用程序时映射所有内容

信息:将 URL 路径 [/register] 映射到处理程序 'userController'

当我返回视图而不是模型时,一切正常。

4

1 回答 1

0

你确定这个:

url : context.get('root') + '/register',

正在返回您的正确网址吗?<%=request.getContextPath()%>/register如果没有,如果您的前端是用 JSP 构建的,您可以改用。

如果还是不行,试试去掉这三个映射:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

并且只使用第一个:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

如果您正在使用/*,则无需声明其余部分。

于 2013-09-04T18:22:34.317 回答