Java 通过Spring使用@Requestmapping的.jsp页面发布表单不起作用
当我尝试在jsp页面中使用submit按钮调用表单post时,页面似乎正在重新加载,但@Requestmapping方法中发生的任何事情都不会被调用 这是Spring注释的java方法:Java 通过Spring使用@Requestmapping的.jsp页面发布表单不起作用,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,当我尝试在jsp页面中使用submit按钮调用表单post时,页面似乎正在重新加载,但@Requestmapping方法中发生的任何事情都不会被调用 这是Spring注释的java方法: @RequestMapping(value = "/TimeTracking.jsp", method = RequestMethod.POST) public String changeTheWeek(HttpServletRequest request) { if ("Previou
@RequestMapping(value = "/TimeTracking.jsp", method = RequestMethod.POST)
public String changeTheWeek(HttpServletRequest request) {
if ("Previous Week".equals(request.getParameter("changeWeek"))) {
this.subtractOneWeek();
} if ("Next Week".equals(request.getParameter("changeWeek")))
{
this.addOneWeek();
}
return "redirect:TimeTracking.jsp";
}
以下是jsp页面的表单:
<form name="ChangeWeek" method="POST" action="TimeTracking.jsp">
<span> <input name="changeWeek" type="submit" value="Previous Week"/> Hours for the week of
<%=currentWeek.firstDayOfThisWeek()%> until <%=currentWeek.lastDayOfThisWeek()%>
<input name="changeWeek" type="submit" value="Next Week"/>
</span>
</form>
最后,这里是表单所在的full-TimeTracking.jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="timeTracking"
class="controllers.TimeTrackingController" scope="request" />
<jsp:useBean id="currentWeek" class="controllers.CurrentWeekController"
scope="request" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Time Tracking Page</title>
<script type="text/javascript" src= "../javascriptpages/timeTracking.js"></script>
</head>
<body>
<div>
<h1>User Time Tracking Page</h1>
</div>
<div id="content">
<form name="ChangeWeek" method="POST" action="TimeTracking.jsp">
<span> <input name="changeWeek" type="submit" value="Previous Week"/> Hours for the week of
<%=currentWeek.firstDayOfThisWeek()%> until <%=currentWeek.lastDayOfThisWeek()%>
<input name="changeWeek" type="submit" value="Next Week"/>
</span>
</form>
<table border="1">
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<tr>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getSunday())%> </td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getMonday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getTuesday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getWednesday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getThursday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getFriday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getSaturday())%></td>
</tr>
</table>
<input type="button" value="<%=timeTracking.displayClockButton()%>"
onClick="clockInOrOutReloadPage()">
</div>
</body>
</html>
在web.xml中,您有*.html扩展名的映射,同时尝试访问映射为.jsp的页面 您需要将映射更改为 @RequestMappingvalue=TimeTracking 并将html表单的操作修复为action=TimeTracking.html
另外,如果您是Spring MVC新手,并且希望使用其功能,请在您的web.xml中检查Spring MVC Showcase demo,您有*.html扩展名的映射,同时尝试访问映射为.jsp的页面 您需要将映射更改为 @RequestMappingvalue=TimeTracking 并将html表单的操作修复为action=TimeTracking.html
另外,如果您是Spring MVC新手,并且希望使用其功能,请查看Spring MVC Showcase demo您的url模式是*.html。因此,将您的操作更改为TimeTracking.html。并将请求映射更改为/TimeTracking.html您的url模式为*.html。因此,将您的操作更改为TimeTracking.html。并将请求映射更改为/TimeTracking.html将SpringServlet的web.xml url模式更改为*或将操作名称更改为TimeTracking.html,并将requestmapping的value属性更改为/TimeTracking.html。从jsp页面我了解到,我们使用输入类型作为按钮,而不是提交,按钮上也有一个事件,尝试更改要提交的输入类型,操作名称和requestmapping的值属性更改为TimeTracking.html。将spring servlet的web.xml url模式更改为*或将操作名称更改为TimeTracking.html,并将requestmapping的值属性更改为/TimeTracking.html。从jsp页面上我了解到,我们使用输入类型作为按钮而不是提交,按钮上也有一个事件,尝试将输入类型更改为提交,将action name和requestmapping的value属性更改为TimeTracking.html。所以我做了这些更改,但仍然不起作用。当我单击submit按钮时,它会给我一个404错误RequestURI=/SpaceTime/jsp pages/TimeTracking.html,但即使我更改了请求映射方法,使其根本不返回时间跟踪页面,它仍然会说它无法解析时间跟踪页面!它进入你的控制器方法了吗?你可以在调试模式下设置断点。我试过了,但它似乎根本没有进入我的控制器方法。所以一定是我的jsp表单写得不正确?最简单的方法是用get替换posr方法,并尝试从浏览器打开页面以查看其映射是否正确-您还可以将org.springframework命名空间的记录器配置为调试级别,最硬的方法是在spring DispatcherServlet Classic中设置断点。您表单中的地址似乎应该以..作为前缀。\n换句话说,您根本不应该通过jsp pages子路径访问spring控制的页面!所以我做了这些改变,但仍然不起作用。当我单击submit按钮时,它会给我一个404错误RequestURI=/SpaceTime/jsp pages/TimeTracking.html,但即使我更改了请求映射方法,使其根本不返回时间跟踪页面,它仍然会说它无法解析时间跟踪页面!它进入你的控制器方法了吗?你可以在调试模式下设置断点。我试过了,但它似乎根本没有进入我的控制器方法。所以一定是我的jsp表单写得不正确?最简单的方法是用get替换posr方法,并尝试从浏览器打开页面以查看其映射是否正确-您还可以将org.springframework命名空间的记录器配置为调试级别,最硬的方法是在spring DispatcherServlet Classic中设置断点。您表单中的地址似乎应该以..作为前缀。\n换句话说,您根本不应该通过jsp pages子路径访问spring控制的页面!
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>jsp-pages/LogIn.jsp</welcome-file>
</welcome-file-list>
</web-app>
package controllers;
import javax.servlet.http.HttpServletRequest;
import models.CurrentWeek;
import models.User;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* this is the controller for current week, it lets you change the current week
* and get values from the current week
*
* @author CorayThan
*
*/
@Controller
public class CurrentWeekController {
@Autowired
private User user;
@Autowired
private CurrentWeek currentWeek;
/**
* @return the user
*/
public User getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(User user) {
this.user = user;
}
/**
* @return the currentWeek
*/
public CurrentWeek getCurrentWeek() {
if (currentWeek==null) {
this.createNewCurrentWeek();
}
return currentWeek;
}
/**
* @param currentWeek the currentWeek to set
*/
public void setCurrentWeek(CurrentWeek currentWeek) {
this.currentWeek = currentWeek;
}
/**
* no arg constructor
*/
public CurrentWeekController() {
}
/**
* this is a post method called when a button is clicked on the time tracking
* jsp page. It reloads the page with a different week
* @param pageWeek
* @param request
* @return
*/
@RequestMapping(value = "/TimeTracking.jsp", method = RequestMethod.POST)
public String changeTheWeek(HttpServletRequest request) {
if ("Previous Week".equals(request.getParameter("changeWeek"))) {
this.subtractOneWeek();
} if ("Next Week".equals(request.getParameter("changeWeek")))
{
this.addOneWeek();
}
return "redirect:TimeTracking.jsp";
}
/**
* this is the default method to show the time tracking page
* @return
*/
// @RequestMapping(value = "/TimeTracking.jsp")
// public ModelAndView showTimeTracking() {
//
// return new ModelAndView("/TimeTracking.jsp", "command", this.getCurrentWeek());
// }
/**
* This creates a current week object by accepting a calendar. If that
* calendar is set to Saturday, it will set all the days in that current
* week object as calendars otherwise it will set all the days in that week
* starting with the current day and counting back to Monday (the first day
* of the week)
*
* @param calendar
* @return
*/
public CurrentWeek createCurrentWeek(MutableDateTime theCurrentDate) {
int day = checkForNull(theCurrentDate);
switch (day) {
case 7:
MutableDateTime sunday = (MutableDateTime) theCurrentDate.clone();
this.getCurrentWeek().setSunday(sunday);
theCurrentDate.addDays(-1);
day--;
case 6:
MutableDateTime saturday = (MutableDateTime) theCurrentDate.clone();
this.getCurrentWeek().setSaturday(saturday);
theCurrentDate.addDays(-1);
day--;
case 5:
MutableDateTime friday = (MutableDateTime) theCurrentDate.clone();
this.getCurrentWeek().setFriday(friday);
theCurrentDate.addDays(-1);
day--;
case 4:
MutableDateTime thursday = (MutableDateTime) theCurrentDate.clone();
this.getCurrentWeek().setThursday(thursday);
theCurrentDate.addDays(-1);
day--;
case 3:
MutableDateTime wednesday = (MutableDateTime) theCurrentDate
.clone();
this.getCurrentWeek().setWednesday(wednesday);
theCurrentDate.addDays(-1);
day--;
case 2:
MutableDateTime tuesday = (MutableDateTime) theCurrentDate.clone();
this.getCurrentWeek().setTuesday(tuesday);
theCurrentDate.addDays(-1);
day--;
case 1:
MutableDateTime monday = (MutableDateTime) theCurrentDate.clone();
this.getCurrentWeek().setMonday(monday);
break;
default:
this.setCurrentWeek(null);
break;
}
return this.getCurrentWeek();
}
/**
* @param theCurrentDate
* @return
*/
private int checkForNull(MutableDateTime theCurrentDate) {
int day = 0;
if (theCurrentDate != null) {
day = theCurrentDate.getDayOfWeek();
}
return day;
}
/**
* makes a new current week
*
* @return
*/
public CurrentWeek createNewCurrentWeek() {
MutableDateTime dateTime = MutableDateTime.now();
CurrentWeek currentWeek = new CurrentWeek();
this.setCurrentWeek(currentWeek);
return createCurrentWeek(dateTime);
}
/**
* subtracts one week from a currentweek
*
*
* @return
*/
public void subtractOneWeek() {
MutableDateTime newMonday = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
newMonday.addDays(-7);
this.setCurrentWeek(createCurrentWeek(newMonday));
}
/**
* adds one week to a currentweek
*
* @param currentWeek
* @return
*/
public void addOneWeek() {
MutableDateTime newMonday = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
newMonday.addDays(7);
this.setCurrentWeek(createCurrentWeek(newMonday));
}
/**
* TODO: make this method into a method that accepts a current week and
* checks if you can add a week to it without going entirely into the future
*
* @param currentWeek
* @return
*/
public CurrentWeek checkIfCurrentWeekIsEntirelyInFuture() {
return this.getCurrentWeek();
}
/**
* returns the first day of the week as a date time
*
* @return
*/
public String firstDayOfThisWeek() {
MutableDateTime firstDay = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
firstDay.addDays(-1);
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("MM/dd/yyyy");
return dateFormatter.print(firstDay);
}
/**
* returns the last day of this week as a date time
*
* @return
*/
public String lastDayOfThisWeek() {
MutableDateTime firstDay = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
firstDay.addDays(5);
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("MM/dd/yyyy");
return dateFormatter.print(firstDay);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="timeTracking"
class="controllers.TimeTrackingController" scope="request" />
<jsp:useBean id="currentWeek" class="controllers.CurrentWeekController"
scope="request" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Time Tracking Page</title>
<script type="text/javascript" src= "../javascriptpages/timeTracking.js"></script>
</head>
<body>
<div>
<h1>User Time Tracking Page</h1>
</div>
<div id="content">
<form name="ChangeWeek" method="POST" action="TimeTracking.jsp">
<span> <input name="changeWeek" type="submit" value="Previous Week"/> Hours for the week of
<%=currentWeek.firstDayOfThisWeek()%> until <%=currentWeek.lastDayOfThisWeek()%>
<input name="changeWeek" type="submit" value="Next Week"/>
</span>
</form>
<table border="1">
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<tr>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getSunday())%> </td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getMonday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getTuesday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getWednesday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getThursday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getFriday())%></td>
<td><%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getSaturday())%></td>
</tr>
</table>
<input type="button" value="<%=timeTracking.displayClockButton()%>"
onClick="clockInOrOutReloadPage()">
</div>
</body>
</html>