无法解析Spring MVC RequestMapping

时间:2015-12-03 13:08:50

标签: spring spring-mvc controller request request-mapping

我想修改我的代码以使其更精简。我的意图如下:

  1. 我写了一个RedirectServlet来处理所有的重定向,所以我使用url-pattern /pages/*来区分原始的dispatcherServlet的请求映射。

  2. 我还将dipatcherServlet的url模式映射到/do/**,因为它由于我不知道的原因而与redirectServlet冲突。但它给我带来了更多需要解决的问题。

  3. 我所有控制器中的@RequestMapping注释可能都有问题。我想指定具有多个分隔符的路径,就像/do/user/signup一样。但是有一个问题,路径无法正确解析,它返回404页面,这让我很沮丧。 类似的东西:

    输入状态报告

    message / do / user / login

    说明请求的资源不可用。

  4. 关于我的说法,我想知道:

    1. 如何配置spring mvc环境,以便我可以重定向到页面而无需在控制器中写入函数,就像:

      @RequestMapping( “submitArticleView”) public String submitArticleView(Model model){     model.addAttribute(“article”,new Article());     返回“submitArticleView”; }

    2. 我想要<a href="xxx.jsp"></a>之类的内容并返回页面。

      1. 如何以精益方式使用@RequestMapping。
      2. 提前致谢。

        我的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>plainart</display-name>
        
          <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
          </welcome-file-list>
        
        
        
          <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/dispatcher-servlet.xml /WEB-INF/applicationContext.xml /WEB-INF/hibernateContext.xml</param-value>
            </context-param>
          <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
          </servlet>
        
        
          <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/do/**</url-pattern>
          </servlet-mapping>
        
        
          <servlet>
            <servlet-name>Redirector</servlet-name>
            <servlet-class>cn.edu.xmu.plainart.controller.Redirector</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Redirector</servlet-name>
            <url-pattern>/pages/*</url-pattern>
        </servlet-mapping>
        
        
          <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
        
           <filter>
                <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
                <filter-class>
                    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
                </filter-class>
            </filter>
        
            <filter-mapping>
                <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
        
          <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
              <param-name>encoding</param-name>
              <param-value>utf-8</param-value>
            </init-param>
          </filter>
          <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>
        
        </web-app>
        

        调度-servlet.xml中

        <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        
        
            <context:annotation-config /> 
            <context:component-scan base-package="cn.edu.xmu.plainart" />
             <mvc:annotation-driven />
        
         <mvc:interceptors> 
                 <mvc:interceptor>
                     <!-- Intercepting specific URL -->
                     <mvc:mapping path="/authenticate/**" />
                     <bean id= "myInterceptor" 
                         class="cn.edu.xmu.plainart.controller.AuthenticationInterceptor" />
                 </mvc:interceptor>
        </mvc:interceptors>
        
        
          <!-- 
        <mvc:resources mapping="/resources/**" location="/resources/theme_default/" />
         -->
        
        <mvc:resources mapping="/resources/**" location="/resources/" />
        
            <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">     
        <!-- one of the properties available; the maximum file size in bytes -->   
        <property name="maxUploadSize" value="100000000"/>  
        </bean>  
        
            <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
                <property name="prefix">
                    <value>/jsp/</value>
                </property>
                <property name="suffix">
                    <value>.jsp</value>
                </property>
            </bean>
        
        </beans>
        

        editorController.java

        @Controller
        @RequestMapping("/do/editor")
        public class EditorController {
        
        
            @Autowired
            private UploadService uploadService;
        
            @Autowired
            private InformationService infoService;
        
            @RequestMapping(value ="/submitArticle", method =RequestMethod.POST)
            public @ResponseBody
            String submitArticle(@ModelAttribute("article") Article article,BindingResult result,HttpServletRequest request){
                Editor editor = (Editor) request.getAttribute("LOGGEDIN_USER");
                article.setAuthor(editor);
                Position pos = new Position("index",10.0);
                pos.appendInfo(article);
                article.setPos(pos);
                ServletContext context = request.getServletContext();
                article = uploadService.uploadArticleInfo(article);
                String path = uploadService.uploadArticle(article.getTitle()+article.getId(), article.getContent(),context);
                System.out.println(path);
                return path;
            }
        
            @RequestMapping(value="/submitAd",method = RequestMethod.POST)
            public @ResponseBody
            String submitAd(@ModelAttribute("ad")Advertisement ad,BindingResult result,@RequestParam("pic") MultipartFile file,HttpServletResponse response, HttpServletRequest request,HttpSession session) throws IllegalStateException, IOException, URISyntaxException{
                Editor editor = (Editor)session.getAttribute("LOGGEDIN_USER");
                ad.setCommitter(editor);
                Position pos = new Position("bottom",5.0);
                pos.appendInfo(ad);
                ad.setPos(pos);
                String name ="/uploads/figure/"+file.getOriginalFilename();
                name = request.getServletContext().getResource(name).toString();
                System.out.println(name);
                File tosave = new File(name);
                file.transferTo(tosave);
                ad.setPic(name);
                uploadService.uploadAdInfo(ad);
                return name;
        
            }
        }
        

0 个答案:

没有答案