Below is the code.
----------------- join.jsp -----------------
<!-- Real-time ID check -->
<script type="text/javascript">
varxmlReq; // specified as a global variable.
// Ajax Object Creation Process
function createAjax() {
xmlReq = new XMLHttpRequest();
}
// Data Transfer Process Using Ajax Objects
function RealTimeIDCheck() {
createAjax();
var m_id = document.getElementById("m_id").value;
xmlReq.onreadystatechange = callBack; // parentheses open and close incorrectly!
xmlReq.open("POST", "id_check_receive.do?m_id=" + m_id, true);
xmlReq.send(null);
// After send, the program continues because it is asynchronous.
}
// Callback function process
function callBack() {
if (xmlReq.readyState == 4) {
if (xmlReq.status == 200) {
printData();
}
}
}
// Process of outputting results
function printData() {
var result = xmlReq.responseXML;
var rootNode = result.documentElement;
// // <root>true</root> , <root>false</root>
var rootValue = rootNode.firstChild.nodeValue;
var rootTag = document.getElementById("result");
var idNode = rootNode.getElementsByTagName("m_id");
var idValue = idNode.item(0).firstChild.nodeValue;
var idTag = document.getElementById("idTxt");
if (rootValue == "true") {
rootTag.innerHTML = "Available ID:";
idTag.innerHTML = "<br>" + idValue;
} } else {
rootTag.innerHTML = "Duplicated ID:";
idTag.innerHTML = "<br>" + idValue;
}
}
</script>
<input type="text" id="m_id" name="m_id" onkeyup="RealTimeIDCheck()" autocomplete="off">
<span id="result" style="color: BLUE;"></span>
<span id="idTxt" style="color: green;"></span>
----------------- MemberController.java -----------------
@RequestMapping(value = "id_check_receive.do", method = RequestMethod.POST)
public void id_check(@RequestParam("m_id") String m_id, HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println ("Do you have an id?" + m_id);
StringBuffer str = new StringBuffer();
str.append("<?xml version='1.0' encoding='utf-8'?>");
str.append("<root>");
int resultNum = Memberservice.idCheck(m_id);
System.out.println ("Do you have an id?" + resultNum);
if (Memberservice.idCheck(m_id) != 0) {
str.append("true");
} } else {
str.append("false");
}
str.append("<id>" + m_id + "</id>");
str.append("</root>");
response.setContentType("text/xml;charset=utf-8");
response.getWriter().write(str.toString());
}
----------------- servlet-context.xml -----------------
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="talent.seok" />
</beans:beans>
----------------- root-context.xml -----------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- Read database.properties -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/database.properties" />
</bean>
<!-- DataSource settings (put as set in database.properties) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:talent/seok/mybatis/mybatis-config.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- Automatically create objects based on annotations -->
<context:annotation-config />
<context:component-scan base-package="talent.seok">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- Multipart Resolaver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- gmail -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="defaultEncoding" value="utf-8" />
<property name="username" value="[email protected]" />
<property name="password" value="rlawlals3" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<bean id="email" class="talent.seok.email.Email">
</bean>
<bean id="emailSender" class="talent.seok.email.EmailSender">
</bean>
</beans>
I'm ashamed that I've been doing it for a few days. Please!
spring ajax
I couldn't test it myselfHowever, it seems to be due to the InternalResourceViewResolver.
Apply the following in order (if it doesn't work in the previous method, use the next method...)
First,
response.getWriter().write(str.toString());
response.getWriter().flush();
// Forces the contents of the buffer to be written to the stream.
}
Second, make HTTP Status respond with 200 OK.
@RequestMapping(value = "id_check_receive.do", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void id_check(@RequestParam("m_id") String m_id, HttpServletRequest request, HttpServletResponse response) throws IOException {
It looks like there's a possibility that the cache is the cause.
Please check whether it is 302 return or 200 return even when you send the first ID value (ex:testtesttest) entered by the browser~
If the cause of the cache is correct, there is a way to change the url requested by timestamp every time.
xmlReq.open("POST", "id_check_receive.do?m_id=" + m_id + "&date=" new Date().getTime(), true);
© 2024 OneMinuteCode. All rights reserved.