Spring登录表单don';t发送参数
我试图修改在线找到的登录示例。 注册页面运行正常 我想登录并返回索引记录。 我的代码没有将参数从LoginController传递到indexSuccess,为什么 页面注册(我没有发布)是相同的,但运行和通信正确 我的密码是这个 有人会知道的请告诉我哪里错了 indexController.javaSpring登录表单don';t发送参数,spring,jsp,spring-mvc,login,Spring,Jsp,Spring Mvc,Login,我试图修改在线找到的登录示例。 注册页面运行正常 我想登录并返回索引记录。 我的代码没有将参数从LoginController传递到indexSuccess,为什么 页面注册(我没有发布)是相同的,但运行和通信正确 我的密码是这个 有人会知道的请告诉我哪里错了 indexController.java package net.codejava.spring.controller; import java.util.Map; import net.codejava.spring.model.Us
package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/index")
public class indexController {
@RequestMapping(method = RequestMethod.GET)
public String viewLogin(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String processIn(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
return "indexSuccess";
}
}
package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@RequestMapping(method = RequestMethod.GET)
public String viewLogin(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
return "Login";
}
@RequestMapping(method = RequestMethod.POST)
public String processLogin(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
return "indexSuccess";
}
}
package net.codejava.spring.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
List<String> professionList = new ArrayList<>();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);
return "Registration";
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
System.out.println("email: " + user.getEmail());
System.out.println("birth date: " + user.getBirthDate());
System.out.println("profession: " + user.getProfession());
return "RegistrationSuccess";
}
}
包net.codejava.spring.controller;
导入java.util.Map;
导入net.codejava.spring.model.User;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
@请求映射(value=“/index”)
公共类索引控制器{
@RequestMapping(method=RequestMethod.GET)
公共字符串viewLogin(地图模型){
用户userForm=新用户();
model.put(“userForm”,userForm);
返回“索引”;
}
@RequestMapping(method=RequestMethod.POST)
公共字符串processIn(@modeldattribute(“userForm”)用户,
地图模型){
System.out.println(“用户名:”+user.getUsername());
System.out.println(“密码:+user.getPassword());
返回“indexSuccess”;
}
}
LoginController.java
package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/index")
public class indexController {
@RequestMapping(method = RequestMethod.GET)
public String viewLogin(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String processIn(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
return "indexSuccess";
}
}
package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@RequestMapping(method = RequestMethod.GET)
public String viewLogin(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
return "Login";
}
@RequestMapping(method = RequestMethod.POST)
public String processLogin(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
return "indexSuccess";
}
}
package net.codejava.spring.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
List<String> professionList = new ArrayList<>();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);
return "Registration";
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
System.out.println("email: " + user.getEmail());
System.out.println("birth date: " + user.getBirthDate());
System.out.println("profession: " + user.getProfession());
return "RegistrationSuccess";
}
}
包net.codejava.spring.controller;
导入java.util.Map;
导入net.codejava.spring.model.User;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
@请求映射(value=“/login”)
公共类登录控制器{
@RequestMapping(method=RequestMethod.GET)
公共字符串viewLogin(地图模型){
用户userForm=新用户();
model.put(“userForm”,userForm);
返回“登录”;
}
@RequestMapping(method=RequestMethod.POST)
公共字符串processLogin(@modeldattribute(“userForm”)用户,
地图模型){
System.out.println(“用户名:”+user.getUsername());
System.out.println(“密码:+user.getPassword());
返回“indexSuccess”;
}
}
RegisterController.java
package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/index")
public class indexController {
@RequestMapping(method = RequestMethod.GET)
public String viewLogin(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String processIn(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
return "indexSuccess";
}
}
package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@RequestMapping(method = RequestMethod.GET)
public String viewLogin(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
return "Login";
}
@RequestMapping(method = RequestMethod.POST)
public String processLogin(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
return "indexSuccess";
}
}
package net.codejava.spring.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
List<String> professionList = new ArrayList<>();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);
return "Registration";
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
System.out.println("email: " + user.getEmail());
System.out.println("birth date: " + user.getBirthDate());
System.out.println("profession: " + user.getProfession());
return "RegistrationSuccess";
}
}
包net.codejava.spring.controller;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
导入net.codejava.spring.model.User;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
@请求映射(value=“/register”)
公共类注册控制器{
@RequestMapping(method=RequestMethod.GET)
公共字符串视图注册(地图模型){
用户userForm=新用户();
model.put(“userForm”,userForm);
List professionList=新建ArrayList();
专业列表。添加(“开发商”);
职业列表。添加(“设计师”);
职业列表。添加(“IT经理”);
模型放置(“专业列表”,专业列表);
返回“注册”;
}
@RequestMapping(method=RequestMethod.POST)
公共字符串processRegistration(@modeldattribute(“userForm”)用户,
地图模型){
System.out.println(“用户名:”+user.getUsername());
System.out.println(“密码:+user.getPassword());
System.out.println(“email:+user.getEmail());
System.out.println(“出生日期:+user.getBirthDate());
System.out.println(“profession:+user.getProfession());
返回“RegistrationSuccess”;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org /tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registrazione e Login</title>
</head>
<body>
<form:form method="post" action="login" commandName="userForm">
<div align="center">
Benvenuto: ${userForm.username}
<table border="1" >
<thead>
<tr>
<th colspan="2">Login </th>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Non sei registrato? <a href="register">Registrati qui</a></td>
</tr>
</tbody>
</table>
</div>
</form:form>
</body>
</html>
登入
Benvenuto:${userForm.username}
登录
用户名
密码
非sei注册?
indexSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Success</title>
</head>
<body>
<div align="center">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Index Successo! </h2></td>
</tr>
<tr>
<td colspan="2" align="center">
<h3>Benvenuto: ${userForm.username}</h3>
</td>
</tr>
</table>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Success</title>
</head>
<body>
<div align="center">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Loggato con Successo! </h2></td>
</tr>
<tr>
<td colspan="2" align="center">
<h3>Benvenuto: ${userForm.username}</h3>
</td>
</tr>
</table>
</div>
</body>
</html>
登录成功
索引成功!
Benvenuto:${userForm.username}
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<div align="center">
<form:form action="login" method="post" commandName="userForm">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Spring MVC Form Demo - Login</h2></td>
</tr>
<tr>
<td>User Name:</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" /></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>
登录
SpringMVC表单演示-登录
用户名:
密码:
loginsAccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Success</title>
</head>
<body>
<div align="center">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Index Successo! </h2></td>
</tr>
<tr>
<td colspan="2" align="center">
<h3>Benvenuto: ${userForm.username}</h3>
</td>
</tr>
</table>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Success</title>
</head>
<body>
<div align="center">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Loggato con Successo! </h2></td>
</tr>
<tr>
<td colspan="2" align="center">
<h3>Benvenuto: ${userForm.username}</h3>
</td>
</tr>
</table>
</div>
</body>
</html>
登录成功
祝你成功!
Benvenuto:${userForm.username}
index-servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <context:annotation-config /> -->
<!-- <context:component-scan base-package="net.codejava.spring.controller.*" /> -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
代码中有两个错误
模型
数据添加到请求
commandName
属性和