Struts2简单入门实例
2013-11-14 16:51
447 查看
安装JDK7以及Tomcat7.0,详细步骤见之前的一篇日志中的前两个步骤,以下是链接:
http://my.oschina.net/zhayefei/blog/106392
测试:浏览器中输入,http://localhost:8080/,若可以看到Tomcat7的欢迎界面,表示Tomcat7配置成功了。
[以下讲解,并不借助Eclipse,便于了解每一个步骤]
在官网上下载struts2的最新版本,我的这个是struts-2.3.15.3,压缩包格式。
在tomcatd的安装路径下,找到webapps/examples,并复制整个examples文件,另取名字为examplesV文件夹。
将步骤2.中的struts2压缩包解压,将/apps中struts2-blank.war解压,复制其WEB-INF/lib下的几个jar包,如下:commons-fileupload、commons-io、commons-lang、commons-logging、freemarker、javassist-3.11.0.GA、ognl、struts2-core-2.3.15.3以及xwork-core-2.3.15.3这9个jar包。
将以上9个jar包复制到Tomcat路径下\webapps\examplesV\WEB-INF\lib中。
实例功能是:首先登陆页面(index.html),需要输入用户名和密码,输入成功则转至欢迎页面(welcome.html),否则转至错误页面(error.html)
在\webapps\examplesV\下面新建三个页面,分别是上述的index.html,welcome.html以及error.html.它们的内容分别如下:
index.html文件内容如下:
注: form的属性action=“Login”,与后文中struts.xml配置一致。
welcome.html文件内容如下:
error.html文件内容如下:
接来下,就是配置web.xml文件,将下载的struts2文件夹中的/apps中struts2-blank.war解压后复制其WEB-INF/下的web.xml文件,到Tomcat7下面的webapps\examplesV\WEB-INF\下,使得web.xml其内容如下:
注:<welcome-file>为设置初始页面,index.html,就是我们的第一个写的登录页面。
写action业务逻辑处理类,java类,命名为LoginAction,负责验证用户名和密码,完成结果页面的跳转。
在cmd命令行下,将LoginAction.java编译成class字节码文件LoginAction. Class.
【因为没有借助eclipse,全部手动实现】cmd-> cd(切换至LoginAction.java的目录),然后javacLoginAction.java
,如果没有报错,则已经成功生成.class文件。
最后,我们将LoginAction.class文件放进Tomcat7下的\webapps\examplesV\WEB-
INF\classes\下面。
配置struts.xml文件。
在Tomcat7下的\webapps\examplesV\WEB-INF\classes\下面,新建struts.xml。
其内容如下:
注:1)action的name属性与index.html中form的action属性一致,都是“Login”
此action动作的Java实现类是,LoginAction。
2)result的name属性“success”与”fail”,分别与LoginAction类中execute()函数中的返回值保持一致,跳转不同的html页面。
开启Tomcat7服务器,并输入localhost:8080/examplesV/,出现登录页面,此时如果 输入zha,密码:900814,则能正常进去欢迎界面,否则进去错误页面。
ps.如果有错误或者不明之处,欢迎指正和提出讨论,谢谢。
http://my.oschina.net/zhayefei/blog/106392
测试:浏览器中输入,http://localhost:8080/,若可以看到Tomcat7的欢迎界面,表示Tomcat7配置成功了。
[以下讲解,并不借助Eclipse,便于了解每一个步骤]
在官网上下载struts2的最新版本,我的这个是struts-2.3.15.3,压缩包格式。
在tomcatd的安装路径下,找到webapps/examples,并复制整个examples文件,另取名字为examplesV文件夹。
将步骤2.中的struts2压缩包解压,将/apps中struts2-blank.war解压,复制其WEB-INF/lib下的几个jar包,如下:commons-fileupload、commons-io、commons-lang、commons-logging、freemarker、javassist-3.11.0.GA、ognl、struts2-core-2.3.15.3以及xwork-core-2.3.15.3这9个jar包。
将以上9个jar包复制到Tomcat路径下\webapps\examplesV\WEB-INF\lib中。
实例功能是:首先登陆页面(index.html),需要输入用户名和密码,输入成功则转至欢迎页面(welcome.html),否则转至错误页面(error.html)
在\webapps\examplesV\下面新建三个页面,分别是上述的index.html,welcome.html以及error.html.它们的内容分别如下:
index.html文件内容如下:
<html> <head> <title>Welcome</title> </head> <body> <formaction="Login"> <!-- 与struts中的配置一一对应--> <td> <tr>username</tr> <inputtype="text" name="username"></input><br/> <tr>password</tr> <inputtype="password" name="password"></input><br/> <inputtype="submit" name="submit"value="submit"></input> </form> </body> </html>
注: form的属性action=“Login”,与后文中struts.xml配置一致。
welcome.html文件内容如下:
<html> <head> </head> <body> <p>Welcome, Leo</p> </body> </html>
error.html文件内容如下:
<html> <body> This is an error page! </body> </html>
接来下,就是配置web.xml文件,将下载的struts2文件夹中的/apps中struts2-blank.war解压后复制其WEB-INF/下的web.xml文件,到Tomcat7下面的webapps\examplesV\WEB-INF\下,使得web.xml其内容如下:
<?xmlversion="1.0" encoding="UTF-8"?> <web-appid="WebApp_9" 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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <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.html</welcome-file> </welcome-file-list> </web-app>
注:<welcome-file>为设置初始页面,index.html,就是我们的第一个写的登录页面。
写action业务逻辑处理类,java类,命名为LoginAction,负责验证用户名和密码,完成结果页面的跳转。
public classLoginAction { private String username; private Stringpassword; public String execute() throws Exception { //pass the value from Form, thisis important. setUsername(username); setPassword(password); if(this.getUsername().equals("zha")&&this.getPassword().equals("900814")) return "success"; else return "fail"; } public String getUsername(){ returnusername; } public void setUsername(Stringusername){ this.username=username; } public StringgetPassword(){ return password; } public voidsetPassword(String password){ this.password=password; } }
在cmd命令行下,将LoginAction.java编译成class字节码文件LoginAction. Class.
【因为没有借助eclipse,全部手动实现】cmd-> cd(切换至LoginAction.java的目录),然后javacLoginAction.java
,如果没有报错,则已经成功生成.class文件。
最后,我们将LoginAction.class文件放进Tomcat7下的\webapps\examplesV\WEB-
INF\classes\下面。
配置struts.xml文件。
在Tomcat7下的\webapps\examplesV\WEB-INF\classes\下面,新建struts.xml。
其内容如下:
<?xmlversion="1.0" encoding="UTF-8" ?> <!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <package name="default" extends="struts-default"namespace="/"> <action name="Login"class="LoginAction"> <resultname="success">/welcome.html</result> <resultname="fail">/error.html</result> </action> </package> </struts>
注:1)action的name属性与index.html中form的action属性一致,都是“Login”
此action动作的Java实现类是,LoginAction。
2)result的name属性“success”与”fail”,分别与LoginAction类中execute()函数中的返回值保持一致,跳转不同的html页面。
开启Tomcat7服务器,并输入localhost:8080/examplesV/,出现登录页面,此时如果 输入zha,密码:900814,则能正常进去欢迎界面,否则进去错误页面。
ps.如果有错误或者不明之处,欢迎指正和提出讨论,谢谢。
相关文章推荐
- Java Struts2 框架入门详解(三)简单的实例
- Struts2简单入门实例
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- 三大框架学习:Struts2简单入门(通过一个简单的登陆实例详解)
- Struts2简单配置与入门实例
- 简单Struts2实例(入门级)
- 实例详解java Struts2的配置与简单案例
- java struts2入门学习实例--使用struts2快速实现多个文件上传
- struts2简单入门示例
- SpringMVC 基础教程 简单入门实例
- 使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之一
- 使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之三
- [.Net码农]MVC——入门+最简单的小实例
- Webservice入门简单实例 教程
- Flume的安装和简单的入门实例
- struts2开发文件上传下载的简单实例
- 最简单的Struts2下载图片实例
- jbpm 入门实例(4)—— JBPM简单实例入门
- 【LGame入门学堂015.控件篇】简单控件之《小人跑跑》游戏实例
- vue服务端渲染简单入门实例