重温struts2之第一个struts2程序helloworld
2014-05-06 20:15
489 查看
来编写struts2的第一个程序:helloworld,以下是步骤:
下载struts2的包
可以自己到官网上下载,较大,就不上传了。
新建工程
在Eclipse中新建java工程,并设置class文件放在WEB-INF文件夹的classes下,然后在新建的工程中,在WEB-INF里面新建web.xml文件,也可以直接创建一个web工程,那样就更简单了。web.xml中配置struts2的核心filter:
添加struts2包
注意,不要把struts2的lib下所有的包都添加到WEB-INF下的lib中,添加工程所需要的即可:
还要注意的是,那些包应该是同一个struts2版本下的包,否则可能会出问题。
编写一个JSP登录
ok,这样下来,我们的工程已经可以支持struts2了。接下来就编写一个JSP登录,以下是login.jsp:
以上action属性的值login.action以.action为后缀可以让struts2核心控制器转发到指定的action
需要注意的是,struts2的Web应用默认需要java5运行环境,需要Web容器支持Servlet API和JSP API。
编写action类
接下来就是写action类了,LoginAction:
极为简单的一个POJO(普通java对象),struts的拦截器机制使得在解析请求参数时,将请求的参数的值赋给相应的action类的属性(反射调用action类的getter()和setter())。所以只要get后面的单词和表单下的单词一样即可,如:jsp中有个username,则action中的方法为getUsername(),够简单吧。
配置action类
写好了action,还要配置它,在classes下新建一个struts.xml:
接下来编写一个welcome.jsp:
如此一来,一切都准备就绪,那么启动tomcat,在浏览器中输入:http://localhost:8080/struts2/helloworld/login.jsp即可看到:
点击登录就可以看到hello world了。
改进action类
可以在action类的execute方法中返回处理结果给JSP页面。
新增一个action用于得到books:
配置action:
其中BookDao只是模拟从数据库获取数据:
showbooks.jsp:
经过这些改进,这个程序便初具MVC模型了。
要说明的是,ActionContext的getSession()返回的并不是一个HttpSession对象,而是一个Map对象,struts2负责两者之间的转换。另外,action属性不仅可以封装请求参数,也可以封装处理结果,struts最终将它处理成struts.valueStack属性放到request中以便JSP页面读取。
使用struts2提供的标签来改进JSP页面:
关于FilterDispatcher再讲一些吧,它作为filter运行于tomcat中,会拦截所有的请求,这些都是在web.xml中设置的。如果请求后缀是.action的话,请求将转入struts2框架处理,那么在我们的业务action类中并没有和ServletAPI耦合,Request对象又是被如何处理的呢。原来,struts2使用了action代理,它解析了请求,并把参数设置给相应的action属性,所以我们可以直接使用action属性,不用设置。所以,在这方面,struts2使用了AOP面向切面编程,虽然不太理解,但先记下吧。
可以在配置FilterDispatcher时配置初始化参数或常量,如:
config参数可以让struts2加载指定的配置文件,actionPackage可以让struts加载指定包下的action类,配置常量也是类似了。
常量的配置还可以自己写一个文件,然后在struts.xml作为<struts>的子元素<include file=...>包含进来。另外,struts2自动加载jar包下的struts2-XXX.xml,如struts-default.xml,也正是这种方式实现了与其他插件的整合,如在spring-plugin.jar下的struts2-spring-plugin.xml定义了整合sping需要的常量。
另外,struts.properties作为properties文件放在classes下可以被自动加载。或者,作为<struts>的子元素<constant name="..." value="..."/>也是可以的
应该注意,业务逻辑控制器(即用户定义的action)不会对请求做处理,而是调用业务逻辑组件处理请求的。
ok,helloworld就讲到这里
下载struts2的包
可以自己到官网上下载,较大,就不上传了。
新建工程
在Eclipse中新建java工程,并设置class文件放在WEB-INF文件夹的classes下,然后在新建的工程中,在WEB-INF里面新建web.xml文件,也可以直接创建一个web工程,那样就更简单了。web.xml中配置struts2的核心filter:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-parttern>/*</url-parttern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
添加struts2包
注意,不要把struts2的lib下所有的包都添加到WEB-INF下的lib中,添加工程所需要的即可:
还要注意的是,那些包应该是同一个struts2版本下的包,否则可能会出问题。
编写一个JSP登录
ok,这样下来,我们的工程已经可以支持struts2了。接下来就编写一个JSP登录,以下是login.jsp:
<%@page language="java" contentType="text/html; charset=gbk"%> <!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>登录页面</title> </head> <body> <center> <form action="login.action" method="post"> <table> <tr> <td> <p>用户名:</p> </td> <td> <input type="text" name="username" /> </td> </tr> <tr> <td> <p>密码:</p> </td> <td> <input type="text" name="password" /> </td> </tr> <tr> <td> <input type="submit" value="登录" /> </td> <td> <input type="reset" value="重置" /> </td> </tr> </table> </form> </center> </body> </html>
以上action属性的值login.action以.action为后缀可以让struts2核心控制器转发到指定的action
需要注意的是,struts2的Web应用默认需要java5运行环境,需要Web容器支持Servlet API和JSP API。
编写action类
接下来就是写action类了,LoginAction:
public class LoginAction { private String username; private String password; public String execute(){ if("a".equals(this.getUsername())&&"a".equals(this.getPassword())) return "success"; return "error"; } 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; } }
极为简单的一个POJO(普通java对象),struts的拦截器机制使得在解析请求参数时,将请求的参数的值赋给相应的action类的属性(反射调用action类的getter()和setter())。所以只要get后面的单词和表单下的单词一样即可,如:jsp中有个username,则action中的方法为getUsername(),够简单吧。
配置action类
写好了action,还要配置它,在classes下新建一个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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> <action name="login" class="ch1.helloworld.LoginAction"> <result name="success">/helloworld/welcome.jsp</result> <result name="error">/helloworld/login.jsp</result> </action> </package> </struts>
接下来编写一个welcome.jsp:
<%@page language="java" contentType="text/html; charset=UTF-8" %> <html> <body> helloworld </body> </html>
如此一来,一切都准备就绪,那么启动tomcat,在浏览器中输入:http://localhost:8080/struts2/helloworld/login.jsp即可看到:
点击登录就可以看到hello world了。
改进action类
可以在action类的execute方法中返回处理结果给JSP页面。
新增一个action用于得到books:
import com.opensymphony.xwork2.ActionContext; public class GetBooksAction { private String[] books; public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public String execute(){ String user = (String)ActionContext.getContext().getSession().get("username"); if(user!= null && "a".equals(user)){ this.setBooks(new BookDao().getBooks()); return "success"; } return "login"; } }
配置action:
<action name="getbooks" class="ch1.helloworld.GetBooksAction"> <result name="success">/helloworld/showbooks.jsp</result> <result name="login">/helloworld/login.jsp</result> </action>
其中BookDao只是模拟从数据库获取数据:
public class BookDao { public String[] getBooks(){ return new String[]{"java","c++","c"}; } }
showbooks.jsp:
<%@page language="java" contentType="text/html; charset=UTF-8" %> <%@page import="java.util.*,com.opensymphony.xwork2.util.*" %> <html> <body> <table> <% ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack"); String[] books = (String[])vs.findValue("books"); for(String book:books){ %> <tr> <td><%= book %></td> </tr> <%} %> </table> </body> </html>
经过这些改进,这个程序便初具MVC模型了。
要说明的是,ActionContext的getSession()返回的并不是一个HttpSession对象,而是一个Map对象,struts2负责两者之间的转换。另外,action属性不仅可以封装请求参数,也可以封装处理结果,struts最终将它处理成struts.valueStack属性放到request中以便JSP页面读取。
使用struts2提供的标签来改进JSP页面:
<%@page language="java" contentType="text/html; charset=UTF-8" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <body> <table border="1"> <s:iterator value="books" status="index"> <s:if test="index.odd==true"> <tr style="background-color:#00dd00"> <td><s:property/></td> </tr> </s:if> <s:else> <tr style="background-color:#00ddee"> <td><s:property/></td> </tr> </s:else> </s:iterator> </table> </body> </html>
关于FilterDispatcher再讲一些吧,它作为filter运行于tomcat中,会拦截所有的请求,这些都是在web.xml中设置的。如果请求后缀是.action的话,请求将转入struts2框架处理,那么在我们的业务action类中并没有和ServletAPI耦合,Request对象又是被如何处理的呢。原来,struts2使用了action代理,它解析了请求,并把参数设置给相应的action属性,所以我们可以直接使用action属性,不用设置。所以,在这方面,struts2使用了AOP面向切面编程,虽然不太理解,但先记下吧。
可以在配置FilterDispatcher时配置初始化参数或常量,如:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>config</param-name> <param-value>aconfig.xml,bconfig.xml</param-value> </init-param> <init-param> <param-name>actionPackages</param-name> <param-value>a.b.c,a.b.d</param-value> </init-param> </filter>
config参数可以让struts2加载指定的配置文件,actionPackage可以让struts加载指定包下的action类,配置常量也是类似了。
常量的配置还可以自己写一个文件,然后在struts.xml作为<struts>的子元素<include file=...>包含进来。另外,struts2自动加载jar包下的struts2-XXX.xml,如struts-default.xml,也正是这种方式实现了与其他插件的整合,如在spring-plugin.jar下的struts2-spring-plugin.xml定义了整合sping需要的常量。
另外,struts.properties作为properties文件放在classes下可以被自动加载。或者,作为<struts>的子元素<constant name="..." value="..."/>也是可以的
应该注意,业务逻辑控制器(即用户定义的action)不会对请求做处理,而是调用业务逻辑组件处理请求的。
ok,helloworld就讲到这里
相关文章推荐
- 第一个Struts2程序之HelloWorld
- Struts2入门第一个程序HelloWorld
- 【 D3.js 入门系列 — 1 】 第一个程序 HelloWorld
- Struts2教程1:第一个Struts2程序
- 第一个Struts2程序,包含编码设置以及开发模式
- 【 D3.js 入门系列 --- 1 】 第一个程序HelloWorld
- 使用J2WTK2.2编译和运行第一个J2ME的HelloWorld程序
- 创建第一个struts2程序
- AAuto 第一个程序,才不是HelloWorld呢!
- 第一个程序HelloWorld
- 第一个Struts2程序
- 道听途说:第一个struts2程序:hello struts2
- Struts2的HelloWorld程序
- DWR的环境搭建与第一个入门程序HelloWorld
- 第一个struts2程序
- 尴尬的第一个Android程序——HelloWorld
- Android开发-第一个程序Helloworld
- Struts2教程1:第一个Struts2程序
- Struts2入门(1)-第一个Struts2程序
- java学习第一个程序HelloWorld