版本说明:
jdk-version: jdk-7u21-windows-i586
struts-version: struts-2.3.16.3-all
eclipse-version: eclipse-jee-luna-R-win32
tomcat-version: apache-tomcat-7.0.55-windows-x86
开发步骤:
1. 下载Struts2: http://struts.apache.org/;
2. 下载Tomcat:http://tomcat.apache.org/;
3. 下载Eclipse:www.eclipse.org;
4. 打开Eclipse新建工程(Dynamic Web project);
5. 在WEB-INF/lib下添加Struts2包(commons-fileupload-1.3.1.jar,commons-io-2.2.jar,commons-lang3-3.1.jar,freemarker-2.3.19.jar,
javassist-3.11.0.GA.jar,ognl-3.0.6.jar,struts2-core-2.3.16.3.jar,xwork-core-2.3.16.3.jar);
6. 在WEB-INF/lib下添加servlet-api.jar;
7. 在WEB-INF下添加文件web.xml(此文件在新建工程时自动生成,或从Struts样例工程中拷贝);
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2 Demo</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
8. 在WebContent目录下添加jsp页面;
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="p" 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=UTF-8"> <title><p:text name="Struts2 Demo"></p:text></title> </head> <body> <p:form action="login"> <p:textfield name="username" key="user" /> <p:textfield name="password" key="pass" /> <p:submit key="login" /> </p:form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="p" 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=UTF-8"> <title><p:text name="Login Error"></p:text></title> </head> <body> <h1>Login Error</h1> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="p" 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=UTF-8"> <title><p:text name="Login Success"></p:text></title> </head> <body> <h1>Login Success</h1> </body> </html>
9. 添加Struts配置文件src/struts.xml;
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="mess" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="com.ihuning.struts2.demo.login" extends="struts-default"> <action name="login" class="com.ihuning.struts2.demo.login.LoginAction"> <result name="input">/index.jsp</result> <result name="error">/WebPage/login_error.jsp</result> <result name="success">/WebPage/login_success.jsp</result> </action> </package> </struts>
10. 添加国际化支持文件src/mess.propertiemess_zh_CN.properties,src/mess_zh_CN.properties;
mess_zh_CN.properties
loginPage=登录页面
errorPage=失败页面
succPage=成功页面
failTip=登录失败
succTip=登录成功
user=密码
pass=用户名
login=登录
mess_zh_CN.properties
loginPage=\u767B\u5F55\u754C\u9762
errorPage=\u5931\u8D25\u9875\u9762
succPage=\u6210\u529F\u9875\u9762
failTip=\u767B\u5F55\u5931\u8D25
succTip=\u767B\u5F55\u6210\u529F
user=\u7528\u6237\u540D
pass=\u5BC6\u7801
login=\u767B\u5F55
11. 添加Action类以及相关处理类com.ihuning.struts2.demo.login/LoginAction.java,com.ihuning.struts2.demo.login/UserManager.java
package com.ihuning.struts2.demo.login; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1441274772944059243L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { int resultCode = UserManager.getInstance().verifyUser(getUsername(), getPassword()); if (resultCode == 0) { return SUCCESS; } else { return ERROR; } } }
package com.ihuning.struts2.demo.login; public class UserManager { private static UserManager instance = new UserManager(); private UserManager() { } public static UserManager getInstance() { return instance; } public int verifyUser(String username, String password) { if ("ihuning".equals(username) && "ihuning".equals(password)) { return 0; } else { return -1; } } }
12. 文件清单视图;
13. 打开JavaEE开发视图,添加Tomcat服务器并部署应用;
14. 在浏览器输入地址(http://localhost:8080/com.ihuning.struts2.demo/)中测试;
相关推荐
**Struts 2初体验** Struts 2是一款强大的基于MVC(Model-View-Controller)设计模式的Java Web框架,它旨在简化企业级应用程序的开发。作为一个初学者,了解并掌握Struts 2的基本概念和操作流程是至关重要的。 **...
本篇文章将带你初识Struts2,并通过实际操作来体验其工作原理和基本用法。提供的压缩包文件"teststruts2"可能包含了示例代码或配置文件,用于辅助学习。 首先,我们了解Struts2的核心概念: 1. **Action类**:在...
32 Struts 2初体验 33 Struts 2配置详解 34 Struts 2深入 拦截器 35 Struts 2综合应用 36 类型转换和OGNL 37-40 项目案例:在线投票系统 41 jsp servlet struts总结 42 Hibernate 入门 43 Hibernate 关联映射 44 HQL...
总结,Struts2的国际化支持使得开发者能够轻松地为多种语言的用户提供一致的体验。从创建Resource Bundle,配置Struts2,到在Action和视图中使用,整个流程是直观且灵活的。通过理解这些基本概念和实践,你可以为你...
总的来说,"初涉StrutsMenu"这篇博客旨在帮助初学者理解和掌握如何在Struts框架中使用StrutsMenu来构建动态菜单,从而提升Web应用的用户体验和安全性。通过阅读和实践,开发者可以更好地掌握这个工具,并将其应用于...
Chapter07通常会涉及Struts2与Ajax的集成,使Web应用具有更丰富的用户体验。Chapter08可能会介绍如何利用Struts2与其他技术(如Spring、Hibernate等)进行集成,实现更强大的企业级功能。Chapter09可能涵盖了异常...