Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring mvc RESTfull url_Spring_Spring Mvc_Restful Url - Fatal编程技术网

spring mvc RESTfull url

spring mvc RESTfull url,spring,spring-mvc,restful-url,Spring,Spring Mvc,Restful Url,我是春季mvc的新手。因此,我尝试使用RESTFull URL(我认为这是正确的名称) 例如,我想使用如下url: 这意味着我要编辑id为1的用户 但在我的配置下,它不会到达任何控制器。我只能使用以.html结尾的URL 这是我的配置和代码 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="htt

我是春季mvc的新手。因此,我尝试使用RESTFull URL(我认为这是正确的名称) 例如,我想使用如下url: 这意味着我要编辑id为1的用户 但在我的配置下,它不会到达任何控制器。我只能使用以.html结尾的URL

这是我的配置和代码

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3MVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>
        com.opensymphony.module.sitemesh.filter.PageFilter
    </filter-class>
  </filter>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

  <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>*.html</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/resources/*</url-pattern>
  </servlet-mapping>
  <!-- 
  Spring Security
  -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-servlet.xml
            /WEB-INF/security-applicationContext.xml
            /WEB-INF/sprekelia-startup.xml
        </param-value>
    </context-param>

    <filter>
      <filter-name>springSecurityFilterChain</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 
    Spring Security
    -->

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


</web-app>

Spring3MVC
index.jsp
网站
com.opensymphony.module.sitemesh.filter.PageFilter
网站
/*
春天
org.springframework.web.servlet.DispatcherServlet
1.
春天
*.html
资源Servlet
org.springframework.js.resource.ResourceServlet
资源Servlet
/资源/*
上下文配置位置
/WEB-INF/spring-servlet.xml
/WEB-INF/security-applicationContext.xml
/WEB-INF/sprekelia-startup.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
org.springframework.web.context.ContextLoaderListener
SpringServlet

<?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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


   <context:annotation-config/>
    <context:component-scan
        base-package="com.sommer.controller" />
        <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:component-scan base-package="com.sommer.service" />


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <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="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="es"/>
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/sommer"/>
      <property name="username"  value="**"/>
      <property name="password" value="**"/>
    </bean>

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource"
    p:jpaVendorAdapter-ref="jpaAdapter">
        <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>                             
        <property name="persistenceUnitName" value="sommerPersistenceUnit"></property>
    </bean>

     <bean id="jpaAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
    p:database="MYSQL"
    p:showSql="true"
    p:generateDdl="true"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory"/>

    <tx:annotation-driven transaction-manager="transactionManager"/>



</beans>

html

---
---
控制器

@Controller
@RequestMapping("Users")
public class SystemUserController {

    }
    @RequestMapping("/index")
    public ModelAndView index(){
        List<SystemUser> list = userRepository.findAll();

    return new ModelAndView("users/index","users",list);
    }

    @RequestMapping("/edit/{userId}")
    public ModelAndView edit(@PathVariable long userId){
        List<SystemUser> list = userRepository.findAll();

    return new ModelAndView("users/index","users",list);
    }
}
@控制器
@请求映射(“用户”)
公共类SystemUserController{
}
@请求映射(“/index”)
公共模型和视图索引(){
List=userRepository.findAll();
返回新的ModelAndView(“用户/索引”、“用户”、列表);
}
@请求映射(“/edit/{userId}”)
公共模型和视图编辑(@PathVariable long userId){
List=userRepository.findAll();
返回新的ModelAndView(“用户/索引”、“用户”、列表);
}
}
你知道如何解决这个问题吗


提前感谢

只有当您具备以下条件时,您才能将DispatcherServlet注册为HTML文件:

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

春天
*.html
如果您不想进行任何扩展,则必须使用以下内容:

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

春天
/*

(当然,请记住,这将真正匹配所有内容

只有在以下情况下,才能将DispatcherServlet注册到HTML文件:

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

春天
*.html
如果您不想进行任何扩展,则必须使用以下内容:

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

春天
/*

(当然,请记住,这将真正匹配所有内容

好的,但我更改为该配置,但现在得到http错误http 404。有什么想法吗?首先:它现在击中你的控制器了吗?第二:确保您的视图解析器正确配置为处理控制器返回的模型和视图。它正在访问控制器,但似乎不起作用的是这一行返回新的模型和视图(“/users/index”,“users”,list);因此,需要重新配置视图解析器以匹配文件结构。很抱歉,我有这个配置。我找不到错误ok,但我更改为该配置,但现在我得到http错误http 404。有什么想法吗?首先:它现在击中你的控制器了吗?第二:确保您的视图解析器正确配置为处理控制器返回的模型和视图。它正在访问控制器,但似乎不起作用的是这一行返回新的模型和视图(“/users/index”,“users”,list);因此,需要重新配置视图解析器以匹配文件结构。对不起,我有这个配置,我找不到错误