springmvc 前台直接访问@RequestMapping("/show")提示404,后台不报错

相关代码:
springmvc.xml

<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!-- 注解驱动,注册HandlerMapping和HandlerAdapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 设置静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>
applicationcontext.xml:
<!-- 注解扫描,spring扫描service,springmvc扫描控制器 -->
<context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
<!-- 加载database属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!-- SqlSessionFactory -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource"/> 
     <property name="typeAliasesPackage" value="com.bjsxt.pojo"/>
</bean>
<!-- 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.bjsxt.mapper"></property>
    <property name="sqlSessionFactoryBeanName" value="factory"></property>  
</bean>
<!-- 事务 管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property> 
</bean>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="ins*"/>
        <tx:method name="upd*"/>
        <tx:method name="del*"/>
        <tx:method name="*" read-only="true"/>
    </tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
    <aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="mypoint"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
控制器
@Controller

public class MenuController {
@Resource
private MenuService menuServiceImpl;

@RequestMapping("/show")
@ResponseBody
public List<Menu> show(){
    System.out.println("show方法");
    return menuServiceImpl.show();
}

}
web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springMVC前端控制器,入口类 -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- springmvc上下文参数,自启动 -->
    <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>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

3个回答

controller没有返回视图页面,你可以返回一个ModelAndView,setView设置返回页面,addObject设置返回数据

cozzw
cozzw 回复: <script type="text/javascript"> $(function(){ $.post("show",function(data){ var result=""; for(var i=0; i<data.length; i++){ result+="<dl>"; result+="<dt>"+data[i].name+"</dt>"; for(var j=0; j<data[i].children.length; j++){ result+="<dd>"+data[i].children[j].name+"</dd>" } result+="</dl>" } $("body").html(result); }) });
一年多之前 回复
cozzw
cozzw 我是把返回值当做字符串返回给页面,页面再以ajax接收
一年多之前 回复

你controller类在这个com.bjsxt.service.impl包下面吗?

cozzw
cozzw 不是的,controller类在com.bjsxt.controller包下
一年多之前 回复

原因已找到,原因是web.xml文件位置放错了,导致tomcat没有找到

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