0

コードを修正してスリムにしたい。私の意図は以下のとおりです。

  1. すべてのリダイレクトを処理する RedirectServlet を作成したので、url-pattern/pages/*を使用して元の dispatcherServlet のリクエスト マッピングと区別します。

  2. また、dipatcherServlet の URL パターンをマップ/do/**しました。理由が不明なため、redirectServlet と競合するためです。しかし、それは私に解決すべき問題をもたらします。

  3. すべてのコントローラーの@RequestMapping注釈が問題になる可能性があります。のように複数のセパレータでパスを指定したい/do/user/signup。しかし、パスを正しく解決できないという問題があり、404 ページが返され、非常にイライラします。何かのようなもの:

    タイプ ステータス レポート

    メッセージ /do/user/login

    説明 要求されたリソースは利用できません。

私のケースについて、知りたいことがたくさんあります:

  1. 次のように、コントローラーに関数を書き込まずにページにリダイレクトできるように、Spring MVC 環境を構成する方法:

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

私は何かが欲しい<a href="xxx.jsp"></a>とページを返します。

  1. @RequestMapping を無駄のない方法で使用する方法。

前もって感謝します。

私の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;

    }
}
4

0 に答える 0