举一个很简单的例子,用户在登陆窗口中输入用户名和密码,提交以后以xml的方式显示你填写的内容,
具体步骤如下: 1.配置web.xml文件
login
org.springframework.web.servlet.DispatcherServlet
1
login
*.htm
2.配置login.xml文件
class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
views
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
LoginAction
class="LoginAction">
LoginInfo
loginfail
main
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文件 xmlns:xsl="" version="1.0">
7.定义index.html文件
8.运行tomcat 预览: 注: 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下。
做完这一切,你就可以在浏览器里面测试结果了~!~
【责编:admin】
--------------------next---------------------