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){ model.addAttribute("article",new Article()); 返回“提交文章视图”;}

我想要类似的东西<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>

调度程序-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