0

I want to revise my codes to make it leaner. My intentions are as below:

  1. I wrote a RedirectServlet to handle all the redirection, so I use the url-pattern /pages/* to make it distinguish from original dispatcherServlet's request mapping.

  2. I also mapped the url pattern of dipatcherServlet to /do/** since it conflicts with the redirectServlet due to reasons unknown to me. But it brings me more problems to solve.

  3. the @RequestMapping annotation in all my controller might be a problem. I want to specify path with more than one seperator,just like /do/user/signup. But there's a problem there the path can't be resolve correctly, and it returns a 404 page, which frustrates me a great deal. something like:

    type Status report

    message /do/user/login

    description The requested resource is not available.

So much about my casse, I want to know :

  1. how to configure spring mvc environment, so that I can redirect to pages without writes a function in controller,just like :

    @RequestMapping("submitArticleView") public String submitArticleView(Model model){ model.addAttribute("article",new Article()); return "submitArticleView"; }

I want something like <a href="xxx.jsp"></a> and return a page.

  1. how to use @RequestMapping in a lean way.

Thanks in advance.

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

dispatcher-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;

    }
}
NaN
  • 7,441
  • 6
  • 32
  • 51
Gnostikoi
  • 117
  • 2
  • 3
  • 12
  • This question could be useful: http://stackoverflow.com/questions/4140448/difference-between-and-in-servlet-mapping-url-pattern – Rick Slot Dec 03 '15 at 13:11
  • Let the `DispatcherServlet` load the `dispatcher-servlet.xml` and not the `ContextLoaderListener`. Currently for your `DispatcherServlet` there are no handlers configured (regardless what you see in your logs). – M. Deinum Dec 03 '15 at 14:02

0 Answers0