0
私はSpring MVCフレームワークを初めて使用し、2つのフォームを持つhtmlページを設計しました。 1つはログイン(フォームID:loginform)、もう1つはサインアップ(フォームID:signupform)です。コントローラはログインページに対して正しく呼び出されていますが、サインアップでは何も起こらず(コントローラが呼び出されません)、ページはちょうどリフレッシュされます。私は、問題の原因を見つけることができないよここに私のJSPページは次のとおりです。Spring MVC:フォーム送信時にコントローラ関数が呼び出されない
login.jspを
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@page session="true"%>
<html>
<head>
<title>Login Page</title>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
\t href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
\t integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
\t crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet"
\t href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
\t integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
\t crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script
\t src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
\t integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
\t crossorigin="anonymous">
\t
</script>
<spring:url value="/resources/css/mylogin.css" var="mainCss" />
<link href="${mainCss}" rel="stylesheet" />
<%-- \t <link href="<spring:url value='/resources/css/mylogin.css'/>" rel="stylesheet"> --%>
</head>
<body>
\t <div class="container">
\t \t <div id="loginbox" style="margin-top: 50px;"
\t \t \t class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
\t \t \t <div class="panel panel-info">
\t \t \t \t <div class="panel-heading">
\t \t \t \t \t <div class="panel-title">Sign In</div>
\t \t \t \t </div>
\t \t \t \t <div style="padding-top: 30px" class="panel-body">
\t \t \t \t \t <c:if test="${not empty error}">
\t \t \t \t \t \t <!-- Invalid Username Password -->
\t \t \t \t \t \t <div class="error">${error}</div>
\t \t \t \t \t </c:if>
\t \t \t \t \t <c:if test="${not empty msg}">
\t \t \t \t \t \t <!-- Logged out successfully -->
\t \t \t \t \t \t <div class="msg">${msg}</div>
\t \t \t \t \t </c:if>
\t \t \t \t \t <div style="display: none" id="login-alert"
\t \t \t \t \t \t class="alert alert-danger col-sm-12"></div>
\t \t \t \t \t <form id="loginform" class="form-horizontal" role="form"
\t \t \t \t \t \t name="loginform"
\t \t \t \t \t \t action="<c:url value='/j_spring_security_check' />" method='POST'>
\t \t \t \t \t \t <div style="margin-bottom: 25px" class="input-group">
\t \t \t \t \t \t \t <span class="input-group-addon"><i
\t \t \t \t \t \t \t \t class="glyphicon glyphicon-user"></i></span> <input id="login-username"
\t \t \t \t \t \t \t \t type="text" class="form-control" name="username" value=""
\t \t \t \t \t \t \t \t placeholder="username">
\t \t \t \t \t \t </div>
\t \t \t \t \t \t <div style="margin-bottom: 25px" class="input-group">
\t \t \t \t \t \t \t <span class="input-group-addon"><i
\t \t \t \t \t \t \t \t class="glyphicon glyphicon-lock"></i></span> <input id="login-password"
\t \t \t \t \t \t \t \t type="password" class="form-control" name="password"
\t \t \t \t \t \t \t \t placeholder="password">
\t \t \t \t \t \t </div>
\t \t \t \t \t \t <div style="margin-top: 10px" class="form-group">
\t \t \t \t \t \t \t <div class="col-sm-12 controls">
\t \t \t \t \t \t \t \t <input class="btn btn-success" id="btn-login" name="submit"
\t \t \t \t \t \t \t \t \t type="submit" value="LOG IN" /> <input type="hidden"
\t \t \t \t \t \t \t \t \t name="${_csrf.parameterName}" value="${_csrf.token}" />
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </div>
\t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t <div class="col-md-12 control">
\t \t \t \t \t \t \t \t <div
\t \t \t \t \t \t \t \t \t style="border-top: 1px solid #888; padding-top: 15px; font-size: 85%">
\t \t \t \t \t \t \t \t \t Don't have an account! <a href="#"
\t \t \t \t \t \t \t \t \t \t onClick="$('#loginbox').hide(); $('#signupbox').show()">
\t \t \t \t \t \t \t \t \t \t Sign Up Here </a>
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </div>
\t \t \t \t \t </form>
\t \t \t \t </div>
\t \t \t </div>
\t \t </div>
\t \t <div id="signupbox" style="display: none; margin-top: 50px"
\t \t \t class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
\t \t \t <div class="panel panel-info">
\t \t \t \t <div class="panel-heading">
\t \t \t \t \t <div class="panel-title">Sign Up</div>
\t \t \t \t </div>
\t \t \t \t <div class="panel-body">
\t \t \t \t \t <form:form id="signupform" class="form-horizontal" role="form"
\t \t \t \t \t \t method="POST" modelAttribute="userForm"
\t \t \t \t \t \t action="<c:url value='/adduser' />">
\t \t \t \t \t \t <div id="signupalert" style="display: none"
\t \t \t \t \t \t \t class="alert alert-danger">
\t \t \t \t \t \t \t <p>Error:</p>
\t \t \t \t \t \t \t <span></span>
\t \t \t \t \t \t </div>
\t \t \t \t \t \t <form:hidden path="id" />
\t \t \t \t \t \t <spring:bind path="name">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="name" class="col-md-3 control-label">Name</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <form:input path="name" type="text" class="form-control"
\t \t \t \t \t \t \t \t \t \t name="name" placeholder="Name" />
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <spring:bind path="email">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="email" class="col-md-3 control-label">Email</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <form:input path="email" type="text" class="form-control"
\t \t \t \t \t \t \t \t \t \t name="email" placeholder="Email Address" />
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <spring:bind path="username">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="username" class="col-md-3 control-label">Username</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <form:input path="username" type="text" class="form-control"
\t \t \t \t \t \t \t \t \t \t name="username" placeholder="Username" />
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <spring:bind path="password">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="password" class="col-md-3 control-label">Password</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <form:input path="password" type="password"
\t \t \t \t \t \t \t \t \t \t class="form-control" name="passwd" placeholder="Password" />
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <spring:bind path="dob">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="date" class="col-md-3 control-label">Date of
\t \t \t \t \t \t \t \t \t Birth</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <form:input path="dob" type="date" name="bday"
\t \t \t \t \t \t \t \t \t \t class="form-control" />
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <spring:bind path="image">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="pic" class="col-md-3 control-label">Profile
\t \t \t \t \t \t \t \t \t Picture</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <form:input path="image" type="file" name="pic"
\t \t \t \t \t \t \t \t \t \t class="form-control" />
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <spring:bind path="genreName">
\t \t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t \t <label for="genre" class="col-md-3 control-label">Favorite
\t \t \t \t \t \t \t \t \t Genres</label>
\t \t \t \t \t \t \t \t <div class="col-md-9">
\t \t \t \t \t \t \t \t \t <!-- <input type="file" name="pic" class="form-control"> -->
\t \t \t \t \t \t \t \t \t <form:select multiple="" path="genreName" name="genreNames"
\t \t \t \t \t \t \t \t \t \t class="form-control">
\t \t \t \t \t \t \t \t \t \t <c:forEach items="${genres}" var="genre" varStatus="ctr">
\t \t \t \t \t \t \t \t \t \t \t <option id="${genre.id}" value="${genre.name}">${genre.name}</option>
\t \t \t \t \t \t \t \t \t \t </c:forEach>
\t \t \t \t \t \t \t \t \t </form:select>
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </spring:bind>
\t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t <!-- Button -->
\t \t \t \t \t \t \t <div class="col-md-offset-3 col-md-9">
\t \t \t \t \t \t \t \t <input class="btn btn-info" id="tn-signup" name="submit"
\t \t \t \t \t \t \t \t \t type="submit" value="Sign Up" />
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </div>
\t \t \t \t \t \t <div class="form-group">
\t \t \t \t \t \t \t <div class="col-md-12 control">
\t \t \t \t \t \t \t \t <div
\t \t \t \t \t \t \t \t \t style="border-top: 1px solid #888; padding-top: 15px; font-size: 85%">
\t \t \t \t \t \t \t \t \t Already have an account? <a id="signinlink" href="#"
\t \t \t \t \t \t \t \t \t \t onClick="$('#signupbox').hide(); $('#loginbox').show()">
\t \t \t \t \t \t \t \t \t \t Sign In </a>
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </div>
\t \t \t \t \t \t </div>
\t \t \t \t \t </form:form>
\t \t \t \t </div>
\t \t \t </div>
\t \t </div>
\t </div>
</body>
</html>
コントローラー:
package com.moviebase.web.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.moviebase.web.model.user.UserDao;
import com.moviebase.web.model.genre.Genre;
import com.moviebase.web.model.genre.GenreDao;
import com.moviebase.web.model.user.User;
@Controller
public class MainController {
\t
\t @Autowired
\t public UserDao userDao;
\t @Autowired
\t public GenreDao genreDao;
\t @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
\t public ModelAndView defaultPage() {
\t \t ModelAndView model = new ModelAndView();
\t \t model.addObject("title", "Moviebase");
\t \t model.addObject("message", "This is default page!");
\t \t model.setViewName("hello");
\t \t Authentication auth = SecurityContextHolder.getContext().getAuthentication();
\t String username = auth.getName(); //get logged in username
\t \t User user = userDao.findByUsername(username);
\t \t System.out.println(user.toString());
\t \t return model;
\t }
\t @RequestMapping(value = "/admin**", method = RequestMethod.GET)
\t public ModelAndView adminPage() {
\t \t ModelAndView model = new ModelAndView();
\t \t model.addObject("title", "Moviebase");
\t \t model.addObject("message", "This page is for ROLE_ADMIN only!");
\t \t model.setViewName("admin");
\t \t return model;
\t }
\t @RequestMapping(value = "/login", method = RequestMethod.GET)
\t public ModelAndView login(@RequestParam(value = "error", required = false) String error,
\t \t \t @RequestParam(value = "logout", required = false) String logout) {
\t \t
\t \t ModelAndView model = new ModelAndView();
\t \t if (error != null) {
\t \t \t System.out.println(" error:"+error);
\t \t \t model.addObject("error", "Invalid username and password!");
\t \t }
\t \t if (logout != null) {
\t \t \t model.addObject("msg", "You've been logged out successfully.");
\t \t }
\t \t
\t \t List<Genre> genreList = genreDao.getAllGenres();
\t \t model.addObject("genres", genreList);
\t \t
\t \t User user = new User();
\t \t model.addObject("userForm", user);
\t \t
\t \t model.setViewName("login");
\t \t return model;
\t }
\t
\t @RequestMapping(value = "/adduser", method = RequestMethod.POST)
\t public ModelAndView createUser(@ModelAttribute("userForm") User user,
\t \t \t BindingResult result) {
\t \t
// \t \t ModelAndView model = new ModelAndView();
// \t \t model.setViewName("redirect:/");
\t \t System.out.println("Came here");
\t \t userDao.insert(user);
\t \t return new ModelAndView("redirect:/admin");
\t }
\t
\t //for 403 access denied page
\t @RequestMapping(value = "/403", method = RequestMethod.GET)
\t public ModelAndView accesssDenied() {
\t \t ModelAndView model = new ModelAndView();
\t \t
\t \t //check if user is login
\t \t Authentication auth = SecurityContextHolder.getContext().getAuthentication();
\t \t if (!(auth instanceof AnonymousAuthenticationToken)) {
\t \t \t UserDetails userDetail = (UserDetails) auth.getPrincipal();
\t \t \t System.out.println(userDetail);
\t \t
\t \t \t model.addObject("username", userDetail.getUsername());
\t \t \t
\t \t }
\t \t
\t \t model.setViewName("403");
\t \t return model;
\t }
}
のWeb.xml
<web-app id="WebApp_ID" version="2.4"
\t xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\t xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
\t http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
\t <display-name>MovieBase</display-name>
\t <!-- Spring MVC -->
\t <servlet>
\t \t <servlet-name>mvc-dispatcher</servlet-name>
\t \t <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
\t \t <load-on-startup>1</load-on-startup>
\t </servlet>
\t <servlet-mapping>
\t \t <servlet-name>mvc-dispatcher</servlet-name>
\t \t <url-pattern>/</url-pattern>
\t </servlet-mapping>
\t <listener>
\t \t <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
\t </listener>
\t <context-param>
\t \t <param-name>contextConfigLocation</param-name>
\t \t <param-value>
\t \t \t /WEB-INF/spring-security.xml,
\t \t \t /WEB-INF/spring-database.xml
\t \t </param-value>
\t </context-param>
\t <!-- Spring Security -->
\t <filter>
\t \t <filter-name>springSecurityFilterChain</filter-name>
\t \t <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
\t </filter>
\t <filter-mapping>
\t \t <filter-name>springSecurityFilterChain</filter-name>
\t \t <url-pattern>/*</url-pattern>
\t </filter-mapping>
</web-app>
のpom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project
\t xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
\t xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
\t <modelVersion>4.0.0</modelVersion>
\t <groupId>database</groupId>
\t <artifactId>moviebase</artifactId>
\t <packaging>war</packaging>
\t <version>1.0-SNAPSHOT</version>
\t <name>Moviebase</name>
\t <properties>
\t \t <jdk.version>1.7</jdk.version>
\t \t <spring.version>3.2.8.RELEASE</spring.version>
\t \t <spring.security.version>3.2.3.RELEASE</spring.security.version>
\t \t <jstl.version>1.2</jstl.version>
\t \t <mysql.connector.version>5.1.30</mysql.connector.version>
\t </properties>
\t <dependencies>
\t \t <!-- Spring 3 dependencies -->
\t \t <dependency>
\t \t \t <groupId>org.springframework</groupId>
\t \t \t <artifactId>spring-core</artifactId>
\t \t \t <version>${spring.version}</version>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework</groupId>
\t \t \t <artifactId>spring-web</artifactId>
\t \t \t <version>${spring.version}</version>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework</groupId>
\t \t \t <artifactId>spring-webmvc</artifactId>
\t \t \t <version>${spring.version}</version>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework</groupId>
\t \t \t <artifactId>spring-jdbc</artifactId>
\t \t \t <version>${spring.version}</version>
\t \t </dependency>
\t \t <!-- Spring Security -->
\t \t <dependency>
\t \t \t <groupId>org.springframework.security</groupId>
\t \t \t <artifactId>spring-security-web</artifactId>
\t \t \t <version>${spring.security.version}</version>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework.security</groupId>
\t \t \t <artifactId>spring-security-config</artifactId>
\t \t \t <version>${spring.security.version}</version>
\t \t </dependency>
\t \t <!-- Spring Security JSP Taglib -->
\t \t <dependency>
\t \t \t <groupId>org.springframework.security</groupId>
\t \t \t <artifactId>spring-security-taglibs</artifactId>
\t \t \t <version>${spring.security.version}</version>
\t \t </dependency>
\t \t <!-- jstl for jsp page -->
\t \t <dependency>
\t \t \t <groupId>jstl</groupId>
\t \t \t <artifactId>jstl</artifactId>
\t \t \t <version>${jstl.version}</version>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>mysql</groupId>
\t \t \t <artifactId>mysql-connector-java</artifactId>
\t \t \t <version>${mysql.connector.version}</version>
\t \t </dependency>
\t </dependencies>
\t <build>
\t \t <finalName>moviebase</finalName>
\t \t <plugins>
\t \t \t <plugin>
\t \t \t \t <groupId>org.apache.maven.plugins</groupId>
\t \t \t \t <artifactId>maven-compiler-plugin</artifactId>
\t \t \t \t <version>2.3.2</version>
\t \t \t \t <configuration>
\t \t \t \t \t <source>${jdk.version}</source>
\t \t \t \t \t <target>${jdk.version}</target>
\t \t \t \t </configuration>
\t \t \t </plugin>
\t \t \t <plugin>
\t \t \t \t <groupId>org.apache.maven.plugins</groupId>
\t \t \t \t <artifactId>maven-eclipse-plugin</artifactId>
\t \t \t \t <version>2.9</version>
\t \t \t \t <configuration>
\t \t \t \t \t <downloadSources>true</downloadSources>
\t \t \t \t \t <downloadJavadocs>false</downloadJavadocs>
\t \t \t \t \t <wtpversion>2.0</wtpversion>
\t \t \t \t </configuration>
\t \t \t </plugin>
\t \t </plugins>
\t </build>
</project>
MVC-ディスパッチャ-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
\t xmlns:context="http://www.springframework.org/schema/context"
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\t xmlns:mvc="http://www.springframework.org/schema/mvc"
\t xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
\t <context:component-scan base-package="com.moviebase.*" />
\t <bean
\t \t class="org.springframework.web.servlet.view.InternalResourceViewResolver">
\t \t <property name="prefix">
\t \t \t <value>/WEB-INF/pages/</value>
\t \t </property>
\t \t <property name="suffix">
\t \t \t <value>.jsp</value>
\t \t </property>
\t </bean>
\t
\t <mvc:annotation-driven />
\t <mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
ログには、エラーが表示されますか? – reos
ログにエラーはありません。 – Rajat
両方のフォームに同じ方法を適用するaction = " の代わりにaction =" j_security_check "を試してみましょう –
Sanka