初学spring mvc 怎么取不到form的值

aiq 2017-01-11 10:29:22
spring mvc 4.1.6

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></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>
<!-- spring MVC的核心就是DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


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


<!-- 使用默认的注解映射 -->
<mvc:annotation-driven/>

<!-- 自动扫描controller包中的控制器 -->
<context:component-scan base-package="com.mvc.rest"/>

<!-- 视图解析路径配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>


login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<form action="login" method="post">
<label>username:</label><input type="text" name="username" /><br>
<label>password:</label><input type="text" name="password" /><br>
<input type="submit" value="submit" />
</form>
</body>
</html>


package com.mvc.rest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RestConstroller {

public RestConstroller() {}
@RequestMapping(value ="/login", method = RequestMethod.GET)
public ModelAndView regist( String username,String password){
System.out.println(username);
if("admin".equals(username)){
return new ModelAndView("success");
}
return new ModelAndView("fail");
}

}


System.out.println(username);这个地方运行后为null;那个地方有问题,还是配置文件漏了什么?
...全文
292 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
aiq 2017-01-11
  • 打赏
  • 举报
回复
我改成POST,也重新发布了 出现 type Status report message Request method 'GET' not supported description The specified HTTP method is not allowed for the requested resource. 是不是那个地方配置有问题
  • 打赏
  • 举报
回复
试了一下 ,后台改成post可以收到数据。你把你的重新发布一下试试
aiq 2017-01-11
  • 打赏
  • 举报
回复
去掉method,变成 @RequestMapping(value ="/login") 也没有,是null 那怎么改呢,查了,改了好几个地方还是不行
___d 2017-01-11
  • 打赏
  • 举报
回复
前台用post. 后台用get 你不觉得有问题吗?
aiq 2017-01-11
  • 打赏
  • 举报
回复
怎么改?改method = RequestMethod.POST 页面报 type Status report message Request method 'GET' not supported description The specified HTTP method is not allowed for the requested resource. 去掉method,变成 @RequestMapping(value ="/login") 也是这个错误
那年花 2017-01-11
  • 打赏
  • 举报
回复
你页面写的method要与你controller里面的metho一致 一起get或者一起post
ichavin 2017-01-11
  • 打赏
  • 举报
回复
应该是get是没错的,但是我记得以前我写的时候,出现这种情况会报错或者404,
  • 打赏
  • 举报
回复
xiesisi3 2017-01-11
  • 打赏
  • 举报
回复
你页面写的POST Controller里面用的GET 怎么拿得到参数。。。
aiq 2017-01-11
  • 打赏
  • 举报
回复
重新建了个项目又好了,晕死了
aiq 2017-01-11
  • 打赏
  • 举报
回复
我把form action="login" 里的login改成其他名字的,只要不是login即可以了,比如login_sys,java里也对应login_sys,post和get就后了,什么问题?我已经删除,重新部署了,搞不明白
dominicHa 2017-01-11
  • 打赏
  • 举报
回复
不写method应该是不报错的吧
aiq 2017-01-11
  • 打赏
  • 举报
回复
前台get,后台也get,或不写method,可以取到数据; 前台post,后台改post,值为null 不写method,出现错误 type Status report message Request method 'GET' not supported description The specified HTTP method is not allowed for the requested resource.

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧