请帮助我......我使用的是2.5弹簧 我收到了一条错误消息。此
javax.servlet.ServletException:javax.servlet.jsp.JspTagException:既不是BindingResult也不是bean名称的普通目标对象' user'可用作请求属性 这里网址 //localhost:8080/Projectname/Main_login.jsp
堆栈跟踪
severe: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doStartTagInternal(BindTag.java:121)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
at org.apache.jsp.Main_005flogin_jsp._jspService(Main_005flogin_jsp.java from :86)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
**LoginController.java**
package controller;
import java.util.logging.SimpleFormatter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logic.Schedule;
import logic.User;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
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.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.util.WebUtils;
import utils.WebConstants;
@Controller
public class LoginController extends SimpleFormController {
private Schedule scheduleService;
public void setScheduleService(Schedule scheduleService) {
this.scheduleService = scheduleService;
}
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,
Object command,BindException bindException)throws Exception{
User user=(User)command;
try{
User loginUser = this.scheduleService.getUserByUserInfo(user.getUserID(),user.getPassword());
WebUtils.setSessionAttribute(request, WebConstants.USER_KEY, loginUser);
ModelAndView modelAndView =
new ModelAndView(getSuccessView(),"loginUser",loginUser);
return modelAndView;
} catch(DataRetrievalFailureException e) {
bindException.reject("error.login");
return showForm(request,response,bindException);
}
}
}
的applicationContext.xml
<?xml version="1.0" encoding="EUC-KR"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/scheduler</value>
</property>
<property name="username"><value>root</value></property>
<property name="password"><value>1234</value></property>
</bean>
<!-- Calendar -->
<bean id="scheduleService" class="logic.ScheduleImpl">
<property name="userCatalog">
<ref bean="userCatalog"/>
</property>
</bean>
<!-- UserCatalog -->
<bean id="userCatalog" class="logic.UserCatalogImpl">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
<!-- UserDao -->
<bean id="userDao" class="dao.UserDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- Validator -->
<bean id = "loginValidator" class="utils.LoginValidator" />
<bean id = "user" class="logic.User">
</bean>
<!-- MessageSource -->
<bean id="messageSource"
class = "org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list><value>messages</value></list>
</property>
</bean>
</beans>
计划-servlet.xml中
<?xml version="1.0" encoding="EUC-KR" ?>
<!DOCTYPE beans PUBLIC "_//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Handler Mapping -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/Main_login.html">loginController</prop>
</props>
</property>
</bean>
<!-- Controller -->
<bean id="loginController" class="controller.LoginController" >
<property name="sessionForm"><value>false</value></property>
<property name="commandName"><value>user</value></property>
<property name="commandClass"><value>logic.User</value></property>
<property name="validator"><ref bean="loginValidator"/></property>
<property name="formView"><value>Main_login</value></property>
<property name="successView"><value>index</value></property>
<property name="scheduleService"><ref bean="scheduleService"/></property>
</bean>
<!-- ViewResolver -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>WebContent</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
User.java
package logic;
import java.io.Serializable;
public class User implements Serializable{
private String userID;
private String password;
private String userName;
// setter / getter () skip
}
Main_login.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@page session = "false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!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=EUC-KR">
<title>Family Scheduler - login page </title>
</head>
<body>
<table align="center">
<tr>
<td><img src="./image/img_main_log.png" alt="logo"/></td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
<form id="form1" action="Main_login.html" method="post">
<spring:bind path="user">
<font color="red"><c:out value="${status.errorMessage}"/></font>
</spring:bind>
<font color="red"><c:out value="${loginErrorMsg}"/></font>
id:
<spring:bind path="user.userID">
<input type="text"
name="<c:out value ="${status.expression}"/>"
value="<c:out value ="${status.value}"/>"
maxlength="20" style="margin-left:16px" >
<font color="red"><c:out value="${status.errorMessage}"/></font>
</spring:bind> <br/>
p/w:
<spring:bind path="user.password">
<input type="password"
name="<c:out value ="${status.expression}"/>"
value="<c:out value ="${status.value}"/>" maxlength="20">
<font color="red"><c:out value="${status.errorMessage}"/></font>
</spring:bind>
<input type="submit" value="Login"/>
</form>
</td>
</tr>
<tr>
<td align="center">
<a href="Join_Us.html">sing up </a>
</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:1)
看起来您正在提交一个尝试将用户对象绑定到请求的表单。您的请求方法签名需要看起来像:
public String onSubmit(@ModelAttribute("user") User user, BindingResult binding, HttpSession session) {
我认为您还需要在显示表单的初始get调用中的请求中设置User对象。
E.g。
model.addAttribute("user", new User());
查看本教程以获取更多信息:
http://www.javacodegeeks.com/2013/04/spring-mvc-form-tutorial.html