举一个很简单的例子,用户在登陆窗口中输入用户名和密码,提交以后以XML的方式显示你填写的内容,
具体步骤如下:
1.配置web.xml文件
<servlet
<servlet-namelogin</servlet-name
<servlet-class
org.springframework.web.servlet.DispatcherServlet
</servlet-class
<load-on-startup1</load-on-startup
</servlet
<servlet-mapping
<servlet-namelogin</servlet-name
<url-pattern*.htm</url-pattern
</servlet-mapping
2.配置login.xml文件
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
<property name="basename"
<valueviews</value
</property
</bean
<!--Request Mapping --
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
<property name="mappings"
<props
<prop key="/login.htm"LoginAction</prop
</props
</property
</bean
<!---Action Definition--
<bean id="LoginAction"
class="LoginAction"
<property name="commandClass"
<valueLoginInfo</value
</property
<property name="fail_view"
<valueloginfail</value
</property
<property name="sUCcess_view"
<valuemain</value
</property
</bean
3.定义控制类loginAction.Java和commandClass--LoginInfo.java
<---------------------LoginAction----------------------------
public class LoginAction extends SimpleFormController {
protected ModelAndView onSubmit(Object cmd, BindException ex)
throws Exception {
LoginInfo loginInfo = (LoginInfo) cmd;
Map model = new HashMap();
List list = new ArrayList();
list.add(loginInfo);
model.put("loginInfos", list);
return new ModelAndView("login", model);
}
<---------------------LoginAction----------------------------
<---------------------LoginInfo----------------------------
public class LoginInfo {
private String username;
private String passWord;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
<---------------------LoginInfo----------------------------
4.定义自己的viewResolver---LoginView
<---------------------LoginView----------------------------
public class LoginView extends AbstractXsltView {
protected Node createDomNode(Map model, String rootName,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement(rootName);
doc.setRootElement(root);
List loginInfos = (List) model.get("loginInfos");
for (Iterator it = loginInfos.iterator(); it.hasNext();) {
LoginInfo info = (LoginInfo) it.next();
Element a = root.addElement("loginInfo");
a.addElement("username").addText(info.getUsername());
a.addElement("password").addText(info.getPassword());
}
response.setContentType("text/xml");
return new DOMWriter().write(doc);
}
}
<---------------------LoginView----------------------------
5.定义views.properties文件
login.class=LoginView
login.stylesheetLocation=/WEB-INF/xsl/loginInfo.xsl
login.root=loginInfos
6.定义loginInfo.xsl文件
<?xml version="1.0" encoding="GBK"?
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
<xsl:output method="xml" omit-xml-declaration="no"/
<xsl:template match="/"
<loginInfos
<xsl:for-each select="loginInfos/loginInfo"
<loginInfo
<username
<xsl:value-of select="username"/
</username
<password
<xsl:value-of select="password"/
</password
</loginInfo
</xsl:for-each
</loginInfos
</xsl:template
</xsl:stylesheet
7.定义index.Html文件
<html
<body
<form method="POST" action="login.htm"
<p align="left"登录</p
<br
用户名:
<input type="text" name="username"
<br
密 码 :
<input type="password" name="password"
<br
<p
<input type="submit" value="提交" name="B1"
<input type="reset" value="重置" name="B2"
</p
</form
</body
</html
8.运行tomcat 预览:http://localhost:8080/xml/index.html
注:
views.properties放在classes文件夹。loginInfo.xsl放在/WEB-INF/xsl/文件夹。
index.html放在根目录下,xml文件放在/WEB-INF/下,其他的.class文件放在/WEB-INF/classes/下
需要的dom4j-1.6.jar、spring.jar放到/WEB-INF/lib下。
做完这一切,你就可以在浏览器里面测试结果了~!~