未捕获的类型错误: 无法读取 null 的属性'style' - 样式。显示不起作用



我的要求是我有一个包含注册信息的jsp页面。它将提交控制器中的所有值,并返回到相同的"注册.jsp"。在此之前,它正在工作。现在的要求是,一旦它回到同一页面,我在从控制器重定向时设置了一个参数。在 Jsp 中,参数得到验证,如果参数来自匹配的控制器,那么一个"td"将可见,当我第一次将 Jsp 发送到控制器时,它应该不可见。

所以这是代码,"注册.jsp"在WebContent -> WEB-INF -> jsp(folder(

<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 收到所有信息并再次返回"注册.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即getters和setters,

public class SignupPojo {
private String firstname;
private int phone;
//getter setter
}

网络.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>

signup-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的帮助下,我解决了这个问题。解决方案是,由于加载问题,我将"脚本"标签放在正文部分的末尾,就在关闭"正文"之前。我已经改变了 document.getElementById('phone'(.style.display = 'visible'; 自 document.getElementById('phone'(.style.display = 'block';

所以这是"未捕获的类型错误:无法读取 null 的属性'样式'"的解决方案,因此对于像我这样面临相同问题的任何人更改标题。

对于您的问题,非常简单的解决方案就是将脚本代码放在 jsp 文件的末尾。 就是这样。 我首先认为,request.getAttribute的转换是问题所在。 但是看了你的评论后,我明白了,你的DOM剧本不知道。这就是为什么错误是"无法读取 null 的样式 id"的原因
原因是,脚本应该始终写在最后,以便它们可以知道 DOM。 否则,您也可以在开始时使用 window.onload,以便您的脚本查看完整的 DOM。

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

最新更新