spring mvc 访问congtroller却跳转查找jsp,报404错误

web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>

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"
version="3.0">

<!-- log4j日志配置文件 -->

log4jConfigLocation
classpath:/log4j.properties


log4jRefreshInterval
60000

<!-- Spring和mybatis的配置文件 -->



contextConfigLocation

classpath:applicationContext.xml




org.springframework.web.context.request.RequestContextListener


<!-- 字符编码过滤器 -->

CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
utf-8


forceEncoding
true



CharacterEncodingFilter
/*

<!-- 请求过滤器,使请求转化为标准http方法,支持get,post,put,delete -->

HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter


HiddenHttpMethodFilter

mvc


<!-- 监听器 -->

org.springframework.web.context.ContextLoaderListener


org.springframework.web.util.Log4jConfigListener

<!-- servlet配置 -->
<servlet>
 <servlet-name>mvc</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>mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

mvc 配置文件

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
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/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-3.0.xsd">

<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
<context:component-scan base-package="ssm.web.controller" />

<!-- 配置应用控制器 -->


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10240000"/>
</bean>

<!-- FreeMarker环境配置 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <!--freemarker模板位置 -->
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
    <property name="freemarkerSettings">
        <props>
            <prop key="defaultEncoding">UTF-8</prop>
        </props>
    </property>
</bean>

<!--FreeMarker视图解析 -->
<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="contentType" value="text/html; charset=UTF-8"/>
    <property name="prefix" value="/"/>
    <property name="suffix" value=".ftl"/>
</bean>

<!-- 视图控制器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="contentType" value="text/html; charset=UTF-8"/>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>


<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="stringHttpMessageConverter" />
            <ref bean="mappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

<bean id="stringHttpMessageConverter"
    class="org.springframework.http.converter.StringHttpMessageConverter">
    <constructor-arg>
        <bean class="java.nio.charset.Charset" factory-method="forName">
            <constructor-arg type="java.lang.String" value="UTF-8" />
        </bean>
    </constructor-arg>

    <property name="supportedMediaTypes">
        <list>
            <value>text/plain;charset=UTF-8</value>
            <value>text/html;charset=UTF-8</value>
            <value>application/json;charset=UTF-8</value>
            <value>application/x-www-form-urlencoded;charset=UTF-8</value> 
        </list>
    </property>
</bean>

<bean id="mappingJacksonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>application/json;charset=UTF-8</value> 
        </list>
    </property>
</bean>

controller 内容
package ssm.web.controller;

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

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ssm.dao.presisten.model.User;
import ssm.service.UserService;

@Controller
@RequestMapping(value = "/admin/user")
public class UserController {
private static Logger logger = Logger.getLogger(UserController.class);
@Autowired
private UserService userService;

@RequestMapping(value = "getUser")
@ResponseBody
public ModelAndView getById(@RequestParam(value = "id",required = false)String id,
        HttpServletRequest request, HttpServletResponse response){
    User user = userService.getById(id);
    ModelAndView model = new ModelAndView();
    model.addObject("user", user);
    return model;
}

}

当返回值 为 string,如:string url=jsp的名字就能正常访问,
返回类型是,map等等的时候就自动跳转寻找web-inf 下的jsp页面

HTTP Status 404 - /ssm.web/WEB-INF/jsp/admin/user/getUser.jsp

这个jsp肯定是没有的。。。,求各位大神指点指点

2个回答

"/getUser" 试试

谢谢啊,不是那个问题。我试了controller返回值只能返回jsp名称才不会报错,如index.jsp,否则都报404

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