springmvc返回html时转成jsp了报404,怎么同时支持解析jsp和html 10C

配置如下:



    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
            <property name="contentType" value="text/html; charset=utf-8"/>
            <property name="cache" value="true"/>
            <property name="suffix" value=".html"/>
            <property name="order" value="3"/>
    </bean>

    <bean id="viewResolverJsp"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp" />
            <property name="order" value="2"/>
    </bean>


            controller的方法如下:

@RequestMapping("helloHTML")
public String helloHTML(HttpServletRequest request, HttpServletResponse response){
    try{
        System.out.println("Controller开始执行");
    }catch(Exception e){

    }finally{
        return "helloTest";
    }
}


    报错如下:
    HTTP Status 404 - /WEB-INF/view/helloTest.jsp

type Status report

message /WEB-INF/view/helloTest.jsp

description The requested resource is not available.

5个回答

建一个html的解析器并继承 InternalResourceView 后重写 checkResource

 */
public class HtmlResourceView extends InternalResourceView {
     @Override
     public boolean checkResource(Locale locale) {
      File file = new File(this.getServletContext().getRealPath("/") + getUrl());
      return file.exists();// 判断该页面是否存在
     }
}

之后在你的配置文件增加bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">







之后在你的配置文件增加bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">







一直发不出来...

     <bean id="htmlviewResolver"  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="viewClass" value="上面你定义的类的路径"/>
        <property name="order" value="0" />
        <property name="prefix" value="/cms/"/>
        <property name="suffix" value=".html" />  
        <property name="contentType" value="text/html;charset=UTF-8"></property>  
    </bean>

这句删除,每次返回时手动加后缀

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!