请教:一直404错误,到底应该怎么改路径?

baidu_35892047 2016-08-31 03:26:17
完全是copy网上的例子

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-1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

TestController.java
package com.itcount.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class TestController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("hello springmvc");
return new ModelAndView("index.jsp");
}

}


spring-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="testController" name="/hello.do" class="com.itcount.springmvc.TestController"></bean>

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


</beans>

index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
This is my JSP page. <br>
</body>
</html>

我知道404是路径错误,但是不知道怎么改,请教
...全文
790 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangwwip 2016-09-09
  • 打赏
  • 举报
回复
你这项目不完整呀,少下了东西吧
李大白白 2016-09-09
  • 打赏
  • 举报
回复



上图可以看出来, 你的后缀已经写了.jsp, 那么返回modelAndView的时候不需要加.jsp了

还有,请问你是访问index.jsp就404吗? 你的url是什么, index..jsp是放在WEB-INF文件夹里?
Swen程序员 2016-09-09
  • 打赏
  • 举报
回复
~~~每个项目启动都是先访问配置文件的,你这个在tomcat启动估计都报错了。
书生_iit 2016-09-08
  • 打赏
  • 举报
回复
估计不完整, 看看xml文件 一步一步找下去
Gsprinkle 2016-09-03
  • 打赏
  • 举报
回复
<!-- SpringMVC核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 自定义核心控制器配置文件加载目录 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> 给你一个例子参考一下
Gsprinkle 2016-09-03
  • 打赏
  • 举报
回复
你没有配置加载Spring配置的路径,默认路径为src目录下的applicationcontext.xml,而你Src目录下没有此文件,所以会报错
能源恒观 2016-09-03
  • 打赏
  • 举报
回复
web.xml中spring配置文件没有配置进去
土土 2016-09-02
  • 打赏
  • 举报
回复
context 文件没有 你复制了项目 但是没有建立context文件 这个你可以手动建 然后放到-tomcat\conf\Catalina\localhost 底下就可以
xiashany 2016-09-02
  • 打赏
  • 举报
回复
你的web.xml访问spring-servlet.xml这个文件了吗??你可以看看网上其他配置spring的例子
有一只柴犬 2016-08-31
  • 打赏
  • 举报
回复
启动报错的情况下,你访问地址都会404.先检查一下启动报错的问题? 看日志应该是配置文件有问题
qq_33486494 2016-08-31
  • 打赏
  • 举报
回复
给你个建议,你先确定一下你在网上拷贝的项目完不完整吧
baidu_35892047 2016-08-31
  • 打赏
  • 举报
回复
引用 1 楼 TTWiFi 的回复:
你的conf文件夹在哪,你的applicationContext.xml在哪?
我没写这个文件,这个是干什么的,写在哪?
  • 打赏
  • 举报
回复
看情况,是配置文件没找到
今天晴 2016-08-31
  • 打赏
  • 举报
回复
你的conf文件夹在哪,你的applicationContext.xml在哪?

81,094

社区成员

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

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