rijyakurinn
rijyakurinn
采纳率100%
2019-05-06 16:30 阅读 1.8k
已采纳

springMVC怎么可以访问.do和.jsp

结果:访问.do报404错误;图片说明

访问.jsp可以正常访问:
图片说明

项目结构:
图片说明

两个Controller:
图片说明
这个返回值是json格式的。

图片说明

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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>springmvc</display-name>

    <!-- Spring配置 -->
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- DispatcherServlet配置 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>




    <!-- 为了能够处理中文的post请求,再配置一个encodingFilter,以避免post请求中文出现乱码情况 -->
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

springmvc.xml

    <context:component-scan
        base-package="com.web"></context:component-scan>

    <!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->

    <!-- 静态资源(js、image等)的访问 -->
    <mvc:default-servlet-handler />

    <!-- 开启注解 -->
    <mvc:annotation-driven />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

bean.xml

    <!-- 1. 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        省略啦!!!!
    </bean>


    <!-- 2. Spring 对SessionFactory创建支持 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:com.pojo/*.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 3. 事务管理 -->
    <!-- a. 配置事务管理器类(切面类) -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- b. 事务通知 -->

    <tx:advice id="txAdvice" transaction-manager="txManager">

    </tx:advice>

    <!-- c. Aop配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* *Service.*(..))"
            id="pt" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
    </aop:config>

    <!--  公用的配置 
    abstract="true" 表示是一个抽象的bean,不会被实例化; 可以被其他bean继承,就可以省略父bean定义多的属性 -->
    <bean id="baseDao" abstract="true">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- dao配置 -->
    <bean id="userDao" parent="baseDao" class="com.dao.UserDao"></bean>

    <!-- service配置 -->
    <bean id="userService" class="com.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <!-- controller配置 -->
    <bean id="userController" class="com.controller.UserController">
        <property name="userService" ref="userService"></property>
    </bean>
</beans>

请问怎么可以访问.do和.jsp呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    Lei_Da_Gou Lei_Da_Gou 2019-05-06 16:36

    吧@RequestMapping注解中value中得.do去掉,再访问就行了。

    点赞 评论 复制链接分享
  • wangml010 wangml010 2019-05-06 16:48

    你的项目改过名字吗?项目右键-properties ,弹框出来之后搜索web,看看web 的项目名称是什么

    点赞 评论 复制链接分享

相关推荐