有句名言,叫做10000小时成为某一个领域的专家。姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧。
Hour 30
这里有一些java bean 的 风格约定
http://en.wikipedia.org/wiki/JavaBean#JavaBean_conventions
package org.apache.struts.helloworld.model; public class MessageStore { private String message; public MessageStore() { setMessage("Hello Struts User"); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
package org.apache.struts.helloworld.action; import org.apache.struts.helloworld.model.MessageStore; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 1L; private MessageStore messageStore; public String execute() throws Exception { messageStore = new MessageStore(); return SUCCESS; } public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } }
这里的字段和属性也是符合java bean 的,否则的话,view 里面是访问不到这里的数据的。这个也是我在上个例子中发狂的原因了。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello World!</title> </head> <body> <h2> <s:property value="messageStore.message" /> </h2> </body> </html>
这里只用到了s:property 这个tag. 在下一章节中,我们将介绍更多的tag.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> <action name="index"> <result>/index.jsp</result> </action> <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
MVN 的 clean 命令