【Java】springmvc请求controller报404

springmvc框架请求Controller报404,代码配置如下:

dispatcher-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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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

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.xsd">

<!-- 1:对web包中指定包中所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->

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

<mvc:annotation-driven/>

<!-- 静态资源访问 需要在头部引入标签路径 -->

<mvc:resources location="/img/" mapping="/img/**"/>

<mvc:resources location="/js/" mapping="/js/**" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources

in the /WEB-INF/views directory -->

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/" />

<property name="suffix" value=".jsp" />

</bean>

</beans>

applicationContext.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.jpg</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.js</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.css</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>dispatcher</servlet-name>

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

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

HelloWorldController.java

package com.controller;

import com.bean.User;

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.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class HelloWorldController {

@RequestMapping(value="/test", method=RequestMethod.GET)

public String test() {

return "test";

}

@RequestMapping("/login")

public @ResponseBody

User helloWorld(@RequestParam("typeId") int typeId) {

User user= new User();

user.setUsername("zhangsan");

System.out.println(user);

return user;

}

@RequestMapping(value = "/login2", method = RequestMethod.GET)

@ResponseBody

public User login2(@RequestParam("typeId") int typeId) {

User user= new User();

user.setUsername("lisi");

System.out.println(user);

return user;

}

}

【Java】springmvc请求controller报404【Java】springmvc请求controller报404

访问路径:http://localhost:8080/FlyPig/...

找了两天了没找到问题,求大神帮忙,跪谢!

回答

404的问题。。。资源未找到。。。为什么有这个资源(已编辑保存)未找到。。。路径错了或路径写的方式错了

你的tomcat是怎么配置的?idea启动web项目是在ROOT下,要不试试去掉路径里的/FlyPig?

你的视图解析器规定的返回视图是.jsp,而你的根路径下的是html文件,

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/" />

<property name="suffix" value=".jsp" />

</bean>

再有,你的test控制器返回的视图String是"test",而你的文件名是"Test"

你return的是test 应该是在你路径下面有一个test.jsp/test.html

在test()的return语句前打个断点,看能否进入test方法。如果可以进,说明是retrun “test” 出了问题,你配置文件没有配置 test的页面,如果进不了,说明requestMapping配置路径有问题,检查一下路径名有没有写错或漏写。最后提醒一句return "test" 不是返回一个test的字符串

我把项目改成Maven创建的springmvc,然后把css,js,image放到WEB-INF下,报红了,这个改怎么解决呢
【Java】springmvc请求controller报404

Spring MVC 资源是配置在WEB-INF下的

@Controller 也需要配置 @RequestMapping

楼主问了问题就消失啦,看到这么多人回答一个都没回复,404问题很明了了,直接改上return "你要访问页面的详细地址" 例如webcontent 下的一个test.jsp return "forward:test.jsp"

以上是 【Java】springmvc请求controller报404 的全部内容, 来源链接: utcz.com/a/89087.html

回到顶部