Spring2.5
Struts1.3
JQuery1.3.2
1. 业务层服务
package com.chencytttl.service;
/**
* 用户管理接口.
* User: 陈瑞军
* Date: 2009-8-12
* Time: 17:50:41
*/
public interface UserManage {
/**
* 判断用户是否存在。
* @param userName 用户名
* @return 用户是否存在
*/
public abstract boolean userExists(String userName);
}
import com.chencytttl.service.UserManage;
import java.util.List;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/**
* 用户管理服务.
* User: 陈瑞军
* Date: 2009-8-12
* Time: 17:50:59
*/
@Service("userMessageBean")
public class UserMessageBean implements UserManage {
/* 用来存放用户名 */
private List<String> userNames = new ArrayList<String>();
@PostConstruct
public void initUserList() {
userNames.add("chencytttl");
userNames.add("zhangsan");
}
public boolean userExists(String userName) {
if (userNames.contains(userName)) {
return true;
}
return false;
}
}
2. Action类
package com.chencytttl.servlet;
import com.chencytttl.service.UserManage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
/**
* 校验用户是否存在Struts Action.
* User: 陈瑞军
* Date: 2009-8-12
* Time: 17:57:37
*/
@Controller("/validUserExists")
public class ValidUserExistsAction extends Action {
@Resource(name = "userMessageBean")
private UserManage userManage;
@Override
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ValidFrom vf = (ValidFrom) actionForm;
boolean isExists = userManage.userExists(vf.getUserName());
PrintWriter out = httpServletResponse.getWriter();
if (!isExists) {
out.append("User not Exists!");
} else {
out.println("Hello" + vf.getUserName());
}
return null;
}
}
3.Spring配置文件,Struts配置文件以及Web.xml文件,以及Spring和Struts整合的关键
Spring配置文件(applicationContext.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.xsd">
<context:component-scan base-package="com.chencytttl"></context:component-scan>
</beans>
Struts配置文件(struts-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="validForm" type="com.chencytttl.servlet.ValidFrom"></form-bean>
</form-beans>
<global-exceptions></global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action path="/validUserExists" name="validForm" scope="request" [color=red]type="org.springframework.web.struts.DelegatingActionProxy"[/color] validate="true" input="/index.jsp">
</action>
</action-mappings>
</struts-config>
其中,红色部分是Spring和Struts整合的关键(当然只是整合的一种方式)
web配置文件(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 加载spring的BeanFactory -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载Struts的调度核心 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4.Jsp页面(index.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Ajax学习</title>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="ajaxstudy.js"></script>
</head>
<body>
<h4>请输入你的姓名:</h4>
<input type="text" id="userNameText" /><span id="result"></span><br/>
<input type="button" value="提交" onclick="valid()">
</body>
</html>
5. JS脚本(ajaxstudy.js)
function valid() {
$.ajax({
url:'validUserExists.do',
type:'get',
data:'userName=' + $("#userNameText").val(),
error: function() {
alert("error");
},
success: function(data) {
$("#result").text(data).css("color", "blue");
}
});
}