我其实是新手,前几天开始学习struts2的知识。
不知道是因为自己笨还是RP不好,弄了我几天。今天可能是我的人品爆发吧,既然给我整成功了。唉,那个高兴甭提了。几天都在痛苦中煎熬,只是这个煎熬是值得的,我现在就把我的配置成功案例给大家分享下。可能代码写的不专业,在以后的学习中我会注意培养的。我们要学习struts2的开发,首先环境的搭建这个工作是必须的,对于我新手来说,这一步也是最纠结的,因为一旦碰到错误。自己在百度和google拼命的寻求答案,可是就是找不到最终的解释。真是苦逼的新手啊。
先说下我的使用struts2 的配置环境:
IDE:myeclipse8.5
JDK:jdk-6u18-windows-i586.exe其实jdk只要是6.0以上的就行我也不知道原因可能说的不是那么精确只是使用1.6版本以上的不用顾虑这么多其他不必要的麻烦
tomcat:apache-tomcat-6.0.35-windows-x64.gz
Struts2 jar包:1.struts2-core-2.2.3.1.jar
2.xwork-core-2.2.3.1.jar
3.ognl-3.0.1.jar
4.javassist-3.11.0.GA.jar
5.freemarker-2.3.16.jar
6.commons-lang-2.5.jar
7.commons-io-2.0.1.jar
8.commons-fileupload-1.2.2.jar
9.asm-commons-3.1.jar
10.asm-3.1.jar
基本要准备的东西都在上面列着:
下面开始我们第一个struts2项目的测试吧!
创建一个jsp页面:login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="hello.action" method="post"> <label>用户名</label><input type="text" name="user.username"/> <label>密码</label><input type="text" name="user.password"/> <input type="submit" value="登录"/> </form> </body> </html>创建两个接受结果的页面:这个随便你要不要创建我这里就写上吧
loginfail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登录失败! </body> </html>loginsuc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登录成功! </body> </html>编写一个action类
package com.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class loginAction extends ActionSupport{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute(){ if(user.getUsername().equals("admin")&&user.getPassword().equals("admin")){ ActionContext.getContext().getSession().put("user", user.getUsername()); return "success"; } else{ return "error"; } } }写个User类
package com.action; public class User { 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; } }
接下来是配置struts.xml
web.xml
基本上就这些了因为这个只是测试我们的环境是否搭建成功的例子!但是这个我个人觉得很重要,所以你们如果还是不行的,我想你也要静下心了慢慢的分析。虽然可能这样也解决不了你的问题,可是至少要努力去解决。因为,天下没有免费的午餐,要想学会你就必须自己花时间去努力。这次,我配置的成功了,可能有一部分原因是因为我们的运气好转了,另一个原因就是因为我一直在努力的付出! 呵呵不说了测试下吧
我也是刚学习struts2的有机会可以交流下 这个是我的QQ191258966