weixin_42768559
月落乀最红尘
采纳率0%
2018-11-27 07:44

SSM框架中,怎么通过controller实现页面的跳转?

今天接触SSM框架的第四天,后台Mybatis生成dao、entity和mapper后,测试成功,能够正常读写数据库了,但是,在加入页面之后,通过controller不能实现页面的跳转,特求助~~
访问情况:
图片说明

点击跳转后,
图片说明

出现404的错误,个人认为,可能是没有找到controller

代码:
messageController.java

@Controller
public class messageController {
    @RequestMapping("/message/go")
    public String gotest(){
        return "second";
    }
}

index.jsp

  <%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<h2>Hello World!</h2>
<a href="/message/go">点击跳转</a>
<p id="test">Hello World!</p>
<button type="button" onclick="selectUser()">onclick test</button>
</body>
</html>

reach.jsp

  <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>跳到这里了</title>
</head>
<body>
跳转成功!!!
</body>
</html>

文件路径:
图片说明

SpringMVC的配置

 <?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:beans="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-4.3.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">


    <!-- ①:top.xiaofeng包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="top.xiaofeng"/>
    <mvc:annotation-driven/>

    <!-- 静态资源访问 -->
    <!--如果webapp下你新建了文件夹,想访问里面的静态资源,那么就要在这配置一下-->
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

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

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

    <!-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 -->
    <!-- /WEB-INF/views/second.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 拦截器 -->
    <mvc:interceptors>
        <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>

    <!-- 定义无Controller的path<->view直接映射 -->
    <!-- <mvc:view-controller path="/" view-name="redirect:/" /> -->
</beans>

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"
         version="3.0">
    <display-name>mydemo</display-name>
    <!-- 配置编码方式-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 配置springmvc的前端控制器 指向spring-mvc.xml 程序在启动的时候就加载springmvc 可以接受所有请求
    load-on-startup:表示启动容器时初始化该Servlet; -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 可以自定义servlet.xml配置文件的位置和名称,
        默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- 将前端URL请求和后台处理方法controller建立对应关系-->
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 取消对某一类文件的拦截-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.md</url-pattern>
    </servlet-mapping>

    <!-- Spring配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

    <!-- 欢迎页面-->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <!--&lt;!&ndash;404错误展示页面&ndash;&gt;-->
    <!--<error-page>-->
    <!--<error-code>404</error-code>-->
    <!--<location>/404.jsp</location>-->
    <!--</error-page>-->

    <!--设置session失效时间为30分钟 -->
    <session-config>
        <session-timeout>600</session-timeout>
    </session-config>
</web-app>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • qq_31122833 小达哥的垃圾桶 3年前
    @RequestMapping("/message/go")
        public String gotest(){
            return "second";
        }
    

    你这/message/go前往的是second.jsp页面

    点赞 评论 复制链接分享
  • weixin_42768559 月落乀最红尘 3年前

    spring-mvc.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:beans="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-4.3.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    
    
        <!-- ①:top.xiaofeng包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
        <context:component-scan base-package="top.xiaofeng"/>
        <mvc:annotation-driven/>
    
        <!-- 静态资源访问 -->
        <!--如果webapp下你新建了文件夹,想访问里面的静态资源,那么就要在这配置一下-->
        <mvc:resources location="/img/" mapping="/img/**"/>
        <mvc:resources location="/css/" mapping="/css/**"/>
        <mvc:resources location="/js/" mapping="/js/**"/>
    
        <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="mappingJacksonHttpMessageConverter"/>
                </list>
            </property>
        </bean>
    
        <bean id="mappingJacksonHttpMessageConverter"
              class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    
        <!-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 -->
        <!-- /WEB-INF/views/second.jsp -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!-- 拦截器 -->
        <mvc:interceptors>
            <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
        </mvc:interceptors>
    
        <!-- 定义无Controller的path<->view直接映射 -->
        <!-- <mvc:view-controller path="/" view-name="redirect:/" /> -->
    </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"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:beans="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-4.3.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    
        <context:component-scan base-package="top.xiaofeng.service"/>
    
        <!--数据库配置 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
    
        <!-- 数据库连接池 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
            <property name="initialSize" value="1"/>
            <property name="maxActive" value="100"/>
            <property name="maxIdle" value="5"/>
            <property name="maxWait" value="80000"/>
        </bean>
    
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- 配置会话工厂SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"/>
            <property name="mapperLocations" value="classpath:sqlmap/*Mapper.xml"/>
            <property name="typeAliasesPackage" value="top.xiaofeng.model" />
        </bean>
    
        <!-- 在spring容器中配置mapper的扫描器产生的动态代理对象在spring的容器中自动注册,bean的id就是mapper类名(首字母小写)-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 指定扫描包的路径,就是mapper接口的路径,多个包中间以 半角逗号隔开   -->
            <property name="basePackage" value="top.xiaofeng.dao"/>
            <!-- 配置sqlSessionFactoryBeanName -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    </beans>
    

    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"
             version="3.0">
        <display-name>mydemo</display-name>
        <!-- 配置编码方式-->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
        <!-- 配置springmvc的前端控制器 指向spring-mvc.xml 程序在启动的时候就加载springmvc 可以接受所有请求
        load-on-startup:表示启动容器时初始化该Servlet; -->
        <servlet>
            <servlet-name>springServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 可以自定义servlet.xml配置文件的位置和名称,
            默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- 将前端URL请求和后台处理方法controller建立对应关系-->
        <servlet-mapping>
            <servlet-name>springServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- 取消对某一类文件的拦截-->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.md</url-pattern>
        </servlet-mapping>
    
        <!-- Spring配置 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext.xml</param-value>
        </context-param>
    
        <!-- 欢迎页面-->
        <welcome-file-list>
            <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
    
        <!--&lt;!&ndash;404错误展示页面&ndash;&gt;-->
        <!--<error-page>-->
        <!--<error-code>404</error-code>-->
        <!--<location>/404.jsp</location>-->
        <!--</error-page>-->
    
        <!--设置session失效时间为30分钟 -->
        <session-config>
            <session-timeout>600</session-timeout>
        </session-config>
    </web-app>
    
    点赞 评论 复制链接分享
  • qq_36688143 FromNowOnUntilTheEnd 3年前

    在web-inf下的文件不能直接访问,你把跳转后的页面拿出来放在跟index.jsp一个目录就可以了
    这个解决方法是最快的

    点赞 评论 复制链接分享
  • m0_37811653 yaolijian 3年前

    你的springmvc 配置文件呢 配置了2个spring的配置文件 web.xml 监听spring 和springmvc 有错

    点赞 评论 复制链接分享

相关推荐