SpringMVC基于注解@GetMapping报404

@GetMapping莫名其妙报404错误,@RequestMapping工作正常,检查好多地方,最后发现在用来代替spring-config.xml的AppServletConfig配置类里面没有配置@EnableWebMvc

 

配置类AppServletConfig代码如下:

@ComponentScan("com.varlor")
@EnableWebMvc
public class AppServletConfig 
   extends WebMvcConfigurerAdapter{
     @Override
    public void configureViewResolvers(
            ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/pages/",
                ".jsp");
    }
}

 

配置后@GetMapping工作正常

运行时没有错; 部分配置文件 ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> <mvc:annotation-driven/> <context:annotation-config/> <mvc:resources location="/static/" mapping="/static/**"></mvc:resources> ``` Controller部分代码 ```@Controller @RequestMapping("/sysuser") public class SystemController extends BaseController { private final Logger log= LoggerFactory.getLogger(SystemController.class); @Resource private UserService userService; @RequestMapping(value = "/login",method = {RequestMethod.POST,RequestMethod.GET}) public void login(User user,boolean rememberMe) throws IOException{ result.setSuccess(false); //用户登录 User userInfo = userService.getByProerties(new String[]{"loginAccount"}, new String[]{user.getName()},null); if(userInfo==null){ result.setMessage("用户名错误"); super.writeJSON(result); return; } if(!userInfo.getName().equals(new Sha256Hash(user.getPassword()).toHex())){ result.setMessage("密码错误"); super.writeJSON(result); return; } //存入session Subject subject = SecurityUtils.getSubject(); //记得传入明文密码 subject.login(new UsernamePasswordToken(userInfo.getName(), user.getPassword(), rememberMe)); session.setAttribute(USER_SESSION, userInfo); result.setMessage("登录成功"); result.setSuccess(true); super.writeJSON(result); } ``` 前端js部分代码 ``` submitHandler:function(form){ $.ajax({ dataType : "json", url : "${contextPath }/sysuser/login", type : "post", data : $("#login_form").serialize(), success : function(data) { $.alert(data.message); if(data.success){ window.location.href = 'page/main'; } }, error : function (e){ var d = e.responseJSON; if(d){ $.alert(d.message); } } }); return false; //阻止form提交 } ``` <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*:/WEB-INF/applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` ``` 发布项目点击登陆后会 404 ``` http://127.0.0.1:8080/.../sysuser/login ```
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页