首页新闻找找看学习计划

工程启动没有问题,提交表单后报404错误,找不到controller方法

1
悬赏园豆:30 [已关闭问题] 关闭于 2018-03-20 12:02

在搭建一个框架,但是一直找不到controller方法,bean扫描,servlet拦截都
检查过了,网上也查了很多资料,但是问题还是没有解决,请各位帮忙看看。
结构:

一次完整请求的日志:

各种配置文件:

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">
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.commoninfo.user.controller">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <aop:aspectj-autoproxy proxy-target-class="true">
        <aop:include name="controllerAspect"/>
    </aop:aspectj-autoproxy>


    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
    <!-- 支持JSON数据格式 -->
    <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">
    </bean>

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--处理静态资源-->
    <mvc:default-servlet-handler/>
</beans>

spring-mybatis.xml:

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

    <!--引入属性文件-->
    <context:property-placeholder location="classpath:/jdbc.properties"/>

    <context:component-scan base-package="com.commoninfo.user.service"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${database.driver}"/>
        <property name="jdbcUrl" value="${database.url}"/>
        <property name="user" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="minPoolSize" value="1"/>
        <property name="maxPoolSize" value="20"/>
        <property name="maxIdleTime" value="1800"/>
        <property name="acquireIncrement" value="2"/>
        <property name="maxStatements" value="0"/>
        <property name="initialPoolSize" value="2"/>
        <property name="idleConnectionTestPeriod" value="1800"/>
        <property name="acquireRetryAttempts" value="30"/>
        <property name="breakAfterAcquireFailure" value="true"/>
        <property name="testConnectionOnCheckout" value="false"/>
    </bean>

    <!--mybatis文件-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--自动扫描entity目录-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath*:com/commoninfo/user/**/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.commoninfo.user.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置事物的注解方式注入 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

web.xml的内容:

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- Spring-mybatis的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!--字符过滤器-->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

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

    <!--404错误页-->
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/view/404.jsp</location>
    </error-page>
</web-app>

index.jsp的内容:

<%--
  Created by IntelliJ IDEA.
  User: zhulongkun
  Date: 2018/3/18
  Time: 14:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>登录测试</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div align="center">
    <form action="/test/dologin.do" method="post">
        <table>
            <tr>
                <td><label>用户名</label></td>
                <td><label>
                    <input type="text" name="username" style="width: 180px;"/>
                </label></td>
            </tr>
            <tr>
                <td><label>密&nbsp;码</label></td>
                <td><label>
                    <input type="password" name="password" style="width: 180px;"/>
                </label></td>
            </tr>
            <tr>
                <td><input type="submit" name="login" value="登录"/></td>
                <td><input id="registerBtn" type="button" name="register" value="注册"/></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

controller.java:

复制代码
package com.commoninfo.user.controller;

import com.commoninfo.user.entity.User;
import com.commoninfo.user.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhulongkun20@163.com
 * @date 2018/3/18 13:56
 */
@Controller
@RequestMapping("/test")
public class LoginController {
    private static Logger logger = Logger.getLogger(LoginController.class);
    @Resource
    private UserService userService;

    @RequestMapping(value = "/dologin.do")
    public String doLogin(HttpServletRequest httpServletRequest, Model model) {
        User user = userService.getUsersByUsername(
                httpServletRequest.getParameter("username")).get(0);
        logger.info("User的信息为:" + user.toString());
        if (userService.doUserLogin(user)) {
            model.addAttribute("successMsg", "登录成功!");
            model.addAttribute("username", user.getUsername());
            logger.info("successMsg:" + model.containsAttribute("successMsg"));
            logger.info("username:" + model.containsAttribute("username"));
            return "success";
        } else {
            model.addAttribute("failedMsg", "用户名或密码错误!");
            logger.info("failedMsg:" + model.containsAttribute("failedMsg"));
            return "failed";
        }
    }
}

 

复制代码
问题补充:

由于博客园提问设置,不能放过多链接,部分xmsi链接被我删了,但是应该不影响问题

Youyou_2608的主页 Youyou_2608 | 菜鸟二级 | 园豆:204
提问于:2018-03-19 13:19
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册