我的要求是我有一個帶有註冊信息的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屬性'樣式」的解決方案,因此更改標題,面向像我這樣的相同問題的任何人。
這是因爲你的代碼有'style = display:none'而不是'style =「display:none」'? – Ofisora
@Ofisora。不,style =「display:none」不能解決問題。 –
你可以改變'document.getElementById('phone')。style.display ='visible'; document.getElementById('phoneTextBox')。style.display ='visible';'to'... display ='block''? – Ofisora