本实例由版主亲自测试,带实例,回复可见下载。
1,下载struts 2.x :http://struts.apache.org/2.2.3/index.html
2,新建项目,命名为:struts2
3,在下载的包中的lib目录下选择:
commons-logging-1.0.4.jar
ognl-3.0.jar
struts2-core-2.2.1.jar
xwork-core-2.2.1.jar
freemarker-2.3.16.jar
拷贝到lib目录下。
4,在src下新建struts.xml
<?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="struts2" extends="struts-default"> </package> </struts>
5,在web.xml中配置过滤器:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
6,新建登陆页面:login.jsp
<form action="login.action" method="post"> username:<input type="text" name="username"><br/> password:<input type="password" name="password"><br/> <input type="submit" value="submit"> </form>
7,新建pojo:LoginAction.java:
privateString username; privateString password; publicString getPassword() { returnpassword; } publicvoidsetPassword(String password) { this.password= password; } publicString getUsername() { returnusername; } publicvoidsetUsername(String username) { this.username= username; } publicString execute(){ return"success"; }
8,新建结果页面:result.jsp
9,配置struts.xml文件:
<action name="login" class="com.test.LoginAction"> <result>/result.jsp</result> </action>
10,访问:http://localhost:2011/struts2/login.jsp
实例下载(回复可见): |