[标题]:[原]Struts2-入门与配置
[时间]:2009-7-13
[摘要]:Struts2学习之入门与配置
[关键字]:浪曦视频,Struts2应用开发系列,WebWork,Apache
[环境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
下载struts-2.1.6-all.zip : http://struts.apache.org/
手动在Tomcat webapps目录下建立名为StrutsHelloWorld的目录。
加入struts2所需的jar(可在struts-2.1.6-all.zip/lib目录下找到):
/StrutsHelloWorld/WEB-INF/lib/:
struts2-core-2.1.6.jar、
xwork-2.1.2.jar、
freemarker-2.3.13.jar、
ognl-2.6.11.jar、
commons-logging-1.0.4.jar、
commons-fileupload-1.2.1.jar、
commons-io-1.3.2.jar
/StrutsHelloWorld/login.jsp:
<%@page contentType="text/html;charset=GBK" %>
<form method="post" action="login.action">
用户名:<input type="text" name="username" /> <br/>
密码:<input type="text" name="password" /> <br/>
<input type="submit" value="提交">
</form>
/StrutsHelloWorld/src/LoginAction.java:
(可手动编译到/StrutsHelloWorld/WEB-INF/classes目录下)
package wintys.struts2.helloworld;
/**
* Struts2 Action
* @version 2009-7-7
* @author Winty(wintys@gmail.com)
*/
public class LoginAction{
private String username;
private String password;
public String getUsername(){
return this.username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.password = password;
}
public String execute() throws Exception{
return "success";
}
}
/StrutsHelloWorld/result.jsp:
<%@page contentType="text/html;charset=GBK" %>
欢迎:${requestScope.username}:${requestScope.password}
/StrutsHelloWorld/WEB-INF/web.xml:
可参考struts-2.1.6-all.zip/apps/struts2-blank-2.1.6.war中的模板。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>MyStruts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>MyStruts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
/StrutsHelloWorld/WEB-INF/classes/struts.xml:
可参考struts-2.1.6-all.zip/apps/struts2-blank-2.1.6.war中的模板。
<?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>
<package name="MyStruts" extends="struts-default">
<action name="login" class="wintys.struts2.helloworld.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
运行流程示意:
login.jsp => LoginAction => result.jsp
[参考资料]:
《浪曦视频之Struts2应用开发系列》
[附件]:
源代码 :
http://www.blogjava.net/Files/wintys/struts_StrutsHelloWorld.zip
posted on 2009-07-13 22:16
天堂露珠 阅读(360)
评论(1) 编辑 收藏 所属分类:
Struts