在spring MVC框架中显示xml视图
2008-02-23 09:30:46来源:互联网 阅读 ()
举一个很简单的例子,用户在登陆窗口中输入用户名和密码,提交以后以XML的方式显示你填写的内容,
具体步骤如下:
1。配置Web.xml文件
<Servlet>
<servlet-name>login</servlet-name>
<servlet-class>
org.springFramework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>login</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">
<value>views</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">
<value>LoginInfo</value>
</property>
<property name="fail_view">
<value>loginfail</value>
</property>
<property name="success_view">
<value>main</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>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:什么是模式?什么是框架?
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash