我在网上查看了几乎所有与该问题有关的答案,但在我的代码中找不到该问题。
这是我的JSP页面。
<form:form method="POST" commandName="category" modelAttribute="category" action="search_category">
<form:input path="category_name" />
<input type="submit" value="Submit">
</form:form>
当我删除
<form:input path="category_name" />
工作正常。我可以与控制器通信。因此问题与这条线有关。
@Controller
public class SearchCategory {
@Autowired
private CategoryService categoryService;
@RequestMapping(value = "/search_category", method = RequestMethod.POST)
public @ResponseBody String searchCategoryFromDatabase(@ModelAttribute("category") Category category, BindingResult result){
return "something";
}
}
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
这是我的servlet-context.xml
<!-- Set the default page as index.jsp -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Map resources -->
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
<!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
还有我的applicationContext.xml
<!-- Enable @Controller annotation support -->
<mvc:annotation-driven />
<context:annotation-config/>
<!-- Set the default page as index.jsp -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Map resources -->
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
<!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
<context:component-scan base-package="com.XXXX"/>
<!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
username root and blank password. Change below if it's not the case -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/XXXX"/>
<property name="username" value="XXXX"/>
<property name="password" value="XXXX"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<!-- Hibernate Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="packagesToScan">
<array>
<value>com.XXXX</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
我可能在XML文件中做错了。我对于这个spring是新来的-hibernate员工,请耐心等待。谢谢..
这是被抛出的异常
Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'category' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.index_jsp._jspx_meth_form_005finput_005f0(index_jsp.java:208)
at org.apache.jsp.index_jsp._jspx_meth_form_005fform_005f0(index_jsp.java:168)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
如果您要index.jsp
通过进行操作http://localhost:8080/yourapp
,我会假设您对此有<welcome- file>
帮助。
这意味着,index.jsp
Spring无需进行任何预处理即可生成HTML。您正在尝试渲染此
<form:form method="POST" commandName="category" modelAttribute="category" action="search_category">
<form:input path="category_name" />
<input type="submit" value="Submit">
</form:form>
<form:form>
Spring的标签库在哪里。首先,请注意您同时使用commandName
和modelAttribute
。这是多余的。使用一个或另一个,不要同时使用。其次,当您指定其中一个时,标记实现将查找HttpServletRequest
具有指定名称的属性。在您的情况下,没有将这样的属性添加到HttpServletRequest
属性中。这是因为Servlet容器index.jsp
直接转发给您。
代替这样做,创建一个新的@Controller
处理程序方法,该方法将向模型添加属性并转发给index.jsp
视图。
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcomePage(Model model) {
model.addAttribute("category", new Category()); // the Category object is used as a template to generate the form
return "index";
}
你可以摆脱这个
<!-- Set the default page as index.jsp -->
<mvc:view-controller path="/" view-name="index"/>
另外,将所有mvc
配置从applicationContext.xml
文件移动到servlet-context.xml
文件。那就是它的归属。这就是为什么。
当我只留下表显示可用的比萨饼与链接“删除”,一切显示正常,删除也正常工作。 我想表单和控制器@modelAttribute有问题,但我找不到确切的问题,我在controller中添加了与表单相同的属性--“mypiza”,这应该可以工作(至少从我在Google上找到的东西来看)。 有人能告诉我们是什么阻碍了这个应用程序的工作,以及如何解决这个问题吗?如果需要其他配置文件,我可以提供它们。
问题内容: 我在网上查看了几乎所有与该问题有关的答案,但在我的代码中找不到该问题。 这是我的JSP页面。 当我删除 它工作正常。我可以与控制器通信。因此问题与这条线有关。 这是我的web.xml 这是我的servlet-context.xml 还有我的applicationContext.xml 我可能在XML文件中做错了。我对于这个春季是新来的-休眠员工,请耐心等待。谢谢.. 这是被抛出的异常
我正在尝试使用Spring读取我在Twitter和Facebook上找到的状态,并将它们发布到SQLite数据库中。每当我尝试运行我的Spring项目时,我都会得到以下错误: 谢谢你,亚当·巴里
“bean名称'login command'的BindingResult和普通目标对象都不能作为请求属性使用” 这是我新项目的第一个控制器,我在处理xml时遇到了一些问题。我想那都解决了,但如果什么都没看到,我想问题可能就在那里。奇怪的是,所有这些代码几乎都是直接从我的另一个项目复制的,而且它工作得很好。 而且,如果这一点重要的话,我正在运行glassfish。提前道谢! 编辑:网页是/moren
嘿,我是从Spring开始的,但是我发现了这个异常并且无法解决它这是我的jsp 这是我的控制器 这是个例外