页面出现:
HTTP Status 400 - Required String parameter 'loginname' is not present
type Status report
message Required String parameter 'loginname' is not present
description The request sent by the client was syntactically incorrect.
Apache Tomcat/8.0.47
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <welcome-file-list> 7 <welcome-file>hello.jsp</welcome-file> 8 </welcome-file-list> 9 <!-- 加载Spring容器配置 --> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 14 <!-- 设置Spring容器加载所有的配置文件的路径 --> 15 <context-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>classpath*:config/applicationContext.xml</param-value> 18 </context-param> 19 20 <!-- 配置SpringMVC核心控制器 --> 21 <servlet> 22 <servlet-name>springMVC</servlet-name> 23 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 24 25 <init-param> 26 <param-name>contextConfigLocation</param-name> 27 <param-value>classpath*:config/springmvc-config.xml</param-value> 28 </init-param> 29 <!-- 启动加载一次 --> 30 <load-on-startup>1</load-on-startup> 31 </servlet> 32 33 <!--为DispatcherServlet建立映射 --> 34 <servlet-mapping> 35 <servlet-name>springMVC</servlet-name> 36 <url-pattern>/</url-pattern> 37 </servlet-mapping> 38 39 <!-- 防止Spring内存溢出监听器 --> 40 <listener> 41 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 42 </listener> 43 44 <!-- 解决工程编码过滤器 --> 45 <filter> 46 <filter-name>encodingFilter</filter-name> 47 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 48 <init-param> 49 <param-name>encoding</param-name> 50 <param-value>UTF-8</param-value> 51 </init-param> 52 <init-param> 53 <param-name>forceEncoding</param-name> 54 <param-value>true</param-value> 55 </init-param> 56 </filter> 57 58 <filter-mapping> 59 <filter-name>encodingFilter</filter-name> 60 <url-pattern>/*</url-pattern> 61 </filter-mapping> 62 </web-app>
springmvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller --> <context:component-scan base-package="com.controller"/> <!-- 设置默认配置方案 --> <mvc:annotation-driven/> <!-- 使用默认的Servlet来响应静态文件 --> <mvc:default-servlet-handler/> <!-- 定义Spring MVC的拦截器 --> <!-- <mvc:interceptors> <mvc:interceptor> 拦截所有请求 <mvc:mapping path="/*"/> 自定义判断用户权限的拦截类 <bean class="com.interceptor.AuthorizedInterceptor"/> </mvc:interceptor> </mvc:interceptors> --> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <!-- 后缀 --> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 上传文件大小上限,单位为字节(10MB) <property name="maxUploadSize"> <value>10485760</value> </property> 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean> --> </beans>
UserControllers.java
1 package com.controller; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestParam; 9 import org.springframework.web.servlet.ModelAndView; 10 11 import com.entity.User; 12 import com.service.UserService; 13 14 @Controller 15 public class UserControllers { 16 @Autowired 17 private UserService userService; 18 19 @RequestMapping("/login") 20 public ModelAndView login( 21 @RequestParam("loginname") String loginname, 22 @RequestParam("password") String password, 23 HttpSession session,ModelAndView mv 24 ){ 25 System.out.println("loginname:"+loginname); 26 System.out.println("password:"+password); 27 User user=userService.login(loginname,password); 28 if(user!=null) { 29 session.setAttribute("user_session",user); 30 mv.setViewName("redirect:/main"); 31 System.out.println(user); 32 System.out.println("跳转成功"); 33 }else { 34 mv.addObject("message", "登录名或密码错误!请重新输入"); 35 mv.setViewName("forward:/loginForm"); 36 System.out.println("跳转失败"); 37 } 38 return mv; 39 } 40 }
页面找不到,看看你的404页面吧
这个Controller里不用forward吧
最开始那个 就是 404 页面啊
@代码森林: 需要啊 就是 如果用户名和密码不符合数据库的就重新返回登录界面
HTTP Status 400 - Required String parameter 'loginname' is not present
这个不是400错误吗 提示你这个个loginname参数不存在。
那需要怎么解决呢?
@冷月无情: 你对照你的web页面设置的参数改改 。前后对照下是否有什么问题
@两点点水: 那个web.xml里面不存在什么问题啊
@冷月无情: 网页页面哈
@两点点水:jsp页面吗?那个没有问题的