2017-08-12 40 views
0

我的要求是我有一个带有注册信息的jsp页面。它将提交控制器中的所有值并返回到相同的“Signup.jsp”。直到这是它的工作。现在的要求是,一旦它返回到同一页面,我在从控制器重定向的同时设置了一个参数。在Jsp中,参数得到验证,如果参数来自控制器匹配,那么将会看到一个'td',这在第一次将Jsp发送给控制器时不可见。未捕获TypeError:无法读取null属性'样式' - style.display无法正常工作

因此,这里的代码, “signup.jsp” 里面的WebContent - > WEB-INF - > JSP(文件夹)

<html> 
<head> 
<!-- So this script below actually checks if from controller "31012057" 
value is coming or not. If matches then "id=phone" in table will be visible 
which is hidden now --> 

<script type="text/javascript"> 
    var value=<%= request.getAttribute("parameter") %> 
    if(value!=null && value==31012057) 
    { 
    alert("My Phone text box will be visible here......."); 
    document.getElementById('phone').style.display = 'visible'; 
    document.getElementById('phoneTextBox').style.display = 'visible'; 
    } 
</script> 
</head> 
<body> 
<form:form id="signup" method="post" action="signup" modelAttribute="signup" 
commandName="signup"> 

<table> 
<tr> 
<td> 
     <form:label path="firstname">FirstName</form:label> 
</td> 
<td> 
     <form:input path="firstname" name="firstname" 
      id="firstname"/> 
</td> 
</tr> 

<tr> 
<td id="phone" style=display:none> 
     <form:label path="phone">Phone</form:label> 
</td> 
<td id="phoneTextBox" style=display:none> 
     <form:input path="phone" name="phone" id="phone" /> 
</td> 
</tr> 
<tr> 
    <td> 
    <form:button id="register" name="register">Register</form:button> 
    </td> 
    </tr> 
</table> 
</form:form> 
</body> 
</html> 

我看到一些样式错误,但无法得到任何站点中的任何解决方案还关于如何在表单提交成功后让电话号码可见并且控制器从jsp接收所有信息并再次返回“signup.jsp”。

如果有人想执行,那么这里是控制器代码。

@RequestMapping(value = "/signup", method = RequestMethod.POST) 
    public String signupPost(@ModelAttribute("signup") SignupPojo 
    signup,Model model) { 

    int parameter=31012057; 
    model.addAttribute("firstname", signup.getFirstname()); 
    model.addAttribute("parameter", parameter); 
    return "signup"; 

的SignupPojo.java其是getter和setter,

 public class SignupPojo { 

    private String firstname; 
    private int phone; 
    //getter setter 
    } 

符web.xml

<servlet> 
    <servlet-name>signup</servlet-name> 
    <servlet-class> 
    org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>signup</servlet-name> 
    <url-pattern>/signup</url-pattern> 
    </servlet-mapping> 

注册-servlet.xml中这是内部的WebContent - > WEB-INF

 <?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" 
    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"> 

<context:component-scan base-package="GIVE YOUR CONTROLLER PACKAGE NAME "/> 

    <bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean> 
    </beans> 

这将是一个很大的帮助。提前感谢。

============================================== ================================ 好的,这是编辑部分。在@Ofisora和@Tahir Hussain Mir的帮助下,我解决了这个问题。解决的办法是,我已经把'script'标签放在body部分的末尾,就在因为加载问题关闭'body'之前。我改变了 document.getElementById('phone')。style.display ='visible'; 至 document.getElementById('phone')。style.display ='block';

因此,这是“Uncaught TypeError:无法读取null属性'样式”的解决方案,因此更改标题,面向像我这样的相同问题的任何人。

+0

这是因为你的代码有'style = display:none'而不是'style =“display:none”'? – Ofisora

+0

@Ofisora。不,style =“display:none”不能解决问题。 –

+0

你可以改变'document.getElementById('phone')。style.display ='visible'; document.getElementById('phoneTextBox')。style.display ='visible';'to'... display ='block''? – Ofisora

回答

0

针对您的问题的非常简单的解决方案是将脚本代码放在您的jsp文件的末尾。 就是这样。 我首先想到的是,request.getAttribute的转换就是这个问题。 但看了你的意见后,我明白了,你的DOM是不知道的脚本。 这就是为什么错误是“无法读取空样式ID”
原因是,脚本应该总是写在最后,以便他们可以知道DOM。否则,您可以在开始时使用window.onload,以便您的脚本查看完整的DOM。

因此,只需将整个脚本标记放在文件末尾即可。

+0

非常感谢。这worked.You救了我的命,但不知道如何改变位置的作品? –

+0

@SharmisthaSakar我很高兴,它的工作适合你。但是,你为什么不接受我的答案吗? –

+0

我现在做了。再次感谢。 –

相关问题