maven加springmvc搭建web项目一直提示404页面,求解惑

yellowaygj 2017-02-22 05:59:22
web.xml代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.spring.web.gameCenter.controller" />

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>

<mvc:annotation-driven></mvc:annotation-driven>
<!-- 自动扫描(自动注入) -->


<mvc:resources location="WEB-INF/Back/" mapping="WEB-INF/Back/**" />
<mvc:resources location="/assets/" mapping="/assets/**" />



<!-- 视图渲染 jsp/freemaker/velocity-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 制定页面存放的路径 -->
<property name="prefix" value="/WEB-INF/Back/"></property>
<!-- 文件的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>

</beans>

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">


<!-- Dubbo服务接口引用 -->
<import resource="classpath:spring/dubbo/dubbo-consumer.xml"/>
</beans>

spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.spring.web.gameCenter.controller" />

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>

<mvc:annotation-driven></mvc:annotation-driven>
<!-- 自动扫描(自动注入) -->


<mvc:resources location="WEB-INF/Back/" mapping="WEB-INF/Back/**" />
<mvc:resources location="/assets/" mapping="/assets/**" />



<!-- 视图渲染 jsp/freemaker/velocity-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 制定页面存放的路径 -->
<property name="prefix" value="/WEB-INF/Back/"></property>
<!-- 文件的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>

</beans>

controller层代码
package com.spring.web.gameCenter.controller;

import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/adminController")
public class UserController {

@RequestMapping(value="index")
public String index() {
return "index";
}

@RequestMapping("index_v1")
public String index_v1() {
return "index_v1";
}

@RequestMapping("login")
public String login() {
return "login";
}

@RequestMapping("user")
public String user() {
return "user";
}

@RequestMapping("list")
@ResponseBody
public String list(String search,Integer offset,Integer limit,String sort,String order,String filter) throws IOException {
JSONObject jsonObject = new JSONObject(true);

return JSON.toJSONString(jsonObject);
}






@RequestMapping(value = "/testRestful/{id}", method = { RequestMethod.DELETE })
public String testRestfulDelete(Integer id) {
System.out.println("The value which the restful style url is DELETE :"
+ id);
return "success";
}

@RequestMapping(value = "/testRestful/{id}", method = { RequestMethod.PUT })
public String testRestfulPut(Integer id) {
System.out.println("The value which the restful style url is PUT :"
+ id);
return "success";
}

}

需要贴pom文件么。我觉得可能是我spring配置错误了。求指点
...全文
566 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
K_1 2018-12-29
  • 打赏
  • 举报
回复
你这WEB.XML不对吧
yql1986 2017-02-24
  • 打赏
  • 举报
回复
写了一个辅助类 打印所有的@Controller的映射 ,你看一下 有没有映射到 下面的代码都是手打的,难免会有错误,错误的地方你订正一下

@Component
public class RequestMappingAppListener implements ApplicationListener<ContextRefreshedEvent>{
 private static final Logger logger=LoggerFactory.getLogger(RequestMappingAppListener.class);

 @override
 public void onApplicationEvent(ContextRefreshedEvent event){
    if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")){
       return;
    }
   
    ApplicationContext ctx=event.getApplicationContext();
    RequestMappingHandlerMapping rmhm=ctx.getBean(RequestMappingHandlerMapping.class);
    if(rmhm==null) return;

    Map<RequestMappingInfo,HandlerMethod> map=rmhm.getHandlerMethods();
    for(Entry<RequestMapingInfo,HandlerMethod> e:map.entrySet()){
      logger.error(e.getKey().toString()+","+e.getValue().toString());
     }
  
 }



}



青元子 2017-02-24
  • 打赏
  • 举报
回复
参考我博客。
yellowaygj 2017-02-23
  • 打赏
  • 举报
回复
引用 5 楼 qnmdcsdn 的回复:
这是,少了json相关的jar包吧
我已经把json有关的代码都删了 就保留了 @RequestMapping(value="index") public String index() { return "index"; }
  • 打赏
  • 举报
回复
这是,少了json相关的jar包吧
yellowaygj 2017-02-23
  • 打赏
  • 举报
回复
首页换成index.jsp就是这个错误 换成login.jsp就是404.、。
可人兒 2017-02-23
  • 打赏
  • 举报
回复
404是路径请求错误,检查下路径有没写错
ryuugu_rena 2017-02-23
  • 打赏
  • 举报
回复
404是路径请求错误
  • 打赏
  • 举报
回复
贴错误

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧