Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTTP状态500-java.lang.IllegalStateException:bean name';的BindingResult和普通目标对象都不是;myUser';可用作请求属性_Java_Spring - Fatal编程技术网

HTTP状态500-java.lang.IllegalStateException:bean name';的BindingResult和普通目标对象都不是;myUser';可用作请求属性

HTTP状态500-java.lang.IllegalStateException:bean name';的BindingResult和普通目标对象都不是;myUser';可用作请求属性,java,spring,Java,Spring,我有个问题。xml不加载Welcome.jsp,而且我写的controller@RequestMapping值也是正确的,我假设它是URL,并且必须将req映射到提到的jsp Web.xml TeamName.java 我认为你应该返回返回“Welcome”换句话说,操作应该在结尾用/Welcome指示您的一些网页谢谢您的回复!一个考虑因素,即使我尝试在服务器上运行Welcome.Jsp,它也返回状态:404。这与此有关吗控制器视图?为什么要尝试localhost:8080/HealthyCom

我有个问题。xml不加载Welcome.jsp,而且我写的controller@RequestMapping值也是正确的,我假设它是URL,并且必须将req映射到提到的jsp

Web.xml TeamName.java
我认为你应该返回
返回“Welcome”返回“Welcome.jsp”,因为mvc-dispatcher.xml已经为网页创建了后缀,在这种情况下为.jsp


为了进入您在浏览器localhost:8080/Welcome.jsp中输入的页面(当然,如果您使用的是本地主机serwer和端口8080)

@RequestMapping只有在dispatcher中存在相应的HandlerMapping(用于类型级注释)和/或HandlerAdapter(用于方法级注释)时才会被处理。默认情况下是这样。

您能详细说明一下吗?我是否在controller.javaI中丢失了任何内容?我做了以下更改,但未找到SAM错误页面:RequestMapping(value=“/Welcome”,method=RequestMethod.GET)public String showForm(映射模型){return“Welcome”;}@RequestMapping(value=“/Welcome”,method=RequestMethod.POST)public String validateForm(@modeldattribute(“myUser”)@Valid TeamName myUser,BindingResult结果,映射模型){if(result.hasErrors()){返回“欢迎”;}在Jsp中:从错误判断,问题可能出在Jsp表单中
在Jsp:
->换句话说,操作应该在结尾用/Welcome指示您的一些网页谢谢您的回复!一个考虑因素,即使我尝试在服务器上运行Welcome.Jsp,它也返回状态:404。这与此有关吗控制器视图?为什么要尝试localhost:8080/HealthyCommunitySimulator/welcome.jsp?它没有映射,这就是为什么得到404,它应该像base(localhost:8080)+requestMapping from controller(在您的例子中是/welcome)=localhost:8080/welcome我现在在带有路径的上下文中得到Servlet[jsp]的以下错误:Servlet.service()[/HealthyCommunitySimulator]引发异常[java.lang.IllegalStateException:bean名称“myUser”的BindingResult和普通目标对象都不能作为请求属性使用]使用根原因java.lang.IllegalStateException:BindingResult和bean名称“myUser”的普通目标对象都不能作为请求属性。您可以检查编译错误。还可以检查像Web.xml、Welcome.jsp这样的文件名的大小写。这些都应该是在所有小的情况下(Web.xml、Welcome.jsp)。没有编译错误,如下所示:java.lang.IllegalStateException:bean名称“myUser”的BindingResult和普通目标对象都不能作为请求属性使用。您看到的异常是因为缺少模型属性。您应该添加一个
model.put(“myUser”,new myUser());
GET
处理程序方法中,以便表单可以绑定到它。更改为:
<?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" id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>  
</context-param> 
<listener>  
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener> 
<listener>
<listener-lass>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener
</listener-class>
</listener>
<servlet>  
<servlet-name>mvc-dispatcher</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
<load-on-startup>1</load-on-startup>  
</servlet> 
<servlet>
<servlet-name>Tiles Dispatch Servlet</servlet-name>
<servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
</servlet>
<servlet-mapping>  
<servlet-name>mvc-dispatcher</servlet-name>  
<url-pattern>/</url-pattern>  
</servlet-mapping>
<servlet-mapping>
<servlet-name>Tiles Dispatch Servlet</servlet-name>
<url-pattern>*.tiles</url-pattern>
</servlet-mapping>
<welcome-file-list> 
<welcome-file>/WEB-INF/pages/Welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="  
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.dev.controller" />  
<bean  
class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix">  
<value>/WEB-INF/pages/</value>  
</property>  
<property name="suffix">  
<value>.jsp</value>  
</property>  
</bean>  
</beans>
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <tiles:insertDefinition name="myapp.homepage" />
    <html>
    <head>
    </head>
    <body>
    <div id="welcomeTitle" class="title">
    <h1>Health Care Simulation - Welcome</h1>
    </div>
    <div id="centerBody" class="body">
    <form:form method="post" action="Welcome" modelAttribute="myUser">
    Enter Team Name:<font color="red"><form:errors path="teamName" /></font>
    <form:input path="teamName" />
    <input type="submit" value="Submit" />
    </form:form>
    </div>
    </body>
    </html>
@RequestMapping(value = "/welcome")
@Controller
public class WelcomeController {

    @ModelAttribute("myUser")

    public TeamName getLoginForm(@RequestParam String user) {
        System.out.println("test");
        TeamName myUser = new TeamName();
        myUser.setTeamName(user);
        return myUser;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(@ModelAttribute("myUser") TeamName myUser)       
            /*Map model*/ {
        System.out.println("test1");
        //myUser.addAttribute("myUser",myUser ); 
        /*TeamName myUser = new TeamName();
        model.put("myUser", myUser);*/
        return "myUser";
    }
    @RequestMapping(method = RequestMethod.POST)
    public String validateForm(
        @ModelAttribute("myUser") @Valid TeamName myUser,
        BindingResult result, Map model) {
        System.out.println("test1");
        if (result.hasErrors()) {
            return "welcome";
        }

        model.put("myUser", myUser);

        return "success";

    }

}
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class TeamName {

    @NotEmpty
    @Size(min = 1, max = 20)
    private String teamName;

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }
}