最近学习Struts2,阅读一些好的博客。收集有关。
原博文地址:http://blog.csdn.net/zz_mm/article/details/5460397
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2的action必须放在一个指定的包空间下定义
-->
<package
name="default"
extends="struts-default">
<!--
定义处理请求URL为login.action的Action
-->
<action
name="login"
class="org.qiujy.web.struts.action.LoginAction">
<!--
定义处理结果字符串和资源之间的映射关系 -->
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
</struts>
|
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2的action必须放在一个指定的包空间下定义
-->
<package
name="qiujy"
extends="struts-default">
<!--
定义处理请求URL为login.action的Action
-->
<action
name="login"
class="org.qiujy.web.struts2.action.LoginAction">
<!--
定义处理结果字符串和资源之间的映射关系 -->
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
<package
name="my"
extends="struts-default"
namespace="/manage">
<!--
定义处理请求URL为login.action的Action
-->
<action
name="backLogin"
class="org.qiujy.web.struts2.action.LoginAction">
<!--
定义处理结果字符串和资源之间的映射关系 -->
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package></struts>
|
<struts>
<include
file="struts-default.xml"/>
<include
file="struts-user.xml"/>
<include
file="struts-book.xml"/>
<include
file="struts-shoppingCart.xml"/>
......
</struts>
|
除此之外。Struts2框架还包括了一个struts.properties文件,该文件主义了Struts2框架的大量常量属性。但通常推荐也是在struts.xml文件里来配置这些常量属性。
<struts>
......
<constant
name="struts.custom.i18n.resources"
value="messages"/>
</struts>
|
(可是,我们为了方便实现Action。大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。由于此类中实现了非常多的有用接口,提供了非常多默认方法,这些默认方法包含获取国际化信息的方法、数据校验的方法、默认的处理用户请求的方法等。这样能够大大的简化Action的开发。)
(当然,Action类中还能够封装处理结果。把处理结果信息当作一属性,提供相应的getter和setter方法)
package org.qiujy.web.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
/**
*@authorqiujy
*@version1.0
*/
publicclass LoginAction
extends ActionSupport{
private String
userName;
private String
password;
private String
msg; //结果信息属性
/**
*@returnthemsg
*/
public String getMsg() {
returnmsg;
}
/**
*@parammsgthemsgtoset
*/
publicvoid setMsg(String msg) {
this.msg
= msg;
}
/**
*@returntheuserName
*/
public String getUserName() {
returnuserName;
}
/**
*@paramuserNametheuserNametoset
*/
publicvoid setUserName(String userName) {
this.userName
= userName;
}
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
}
/**
*@parampasswordthepasswordtoset
*/
publicvoid setPassword(String password) {
this.password
= password;
}
/**
*处理用户请求的excute()方法
*@return结果导航字符串
*@throwsException
*/
public String execute()
throws Exception{
if("test".equals(this.userName)
&&
"test".equals(this.password)){
msg =
"登录成功。欢迎" +
this.userName;
returnthis.SUCCESS;
}else{
msg =
"登录失败,username或password错";
returnthis.ERROR;
}
}
}
|
Struts2中提供了一个ActionContext类(当前Action的上下文对象),通过这个类能够訪问Servlet API。以下是该类中提供的几个经常用法:
类似于调用HttpServletRequest对象的getParameterMap() 方法。
public String execute()
throws Exception{
if("test".equals(this.userName)
&& "test".equals(this.password)){
msg =
"登录成功,欢迎" +
this.userName;
//获取ActionContext实例。通过它来訪问Servlet
API
ActionContext context = ActionContext.getContext();
//看session中是否已经存放了username,假设存放了:说明已经登录了;
//否则说明是第一次登录成功
if(null
!= context.getSession().get("uName")){
msg =
this.userName +
":你已经登录过了!!!";
}else{
context.getSession().put("uName",
this.userName);
}
returnthis.SUCCESS;
}else{
msg =
"登录失败,username或password错";
returnthis.ERROR;
}
}
|
Struts2也提供了类似的功能。处理方式主要有下面三种方式:
<form method="post" action="userOpt!login.action">
|
package org.qiujy.web.struts2.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*@authorqiujy
*@version1.0
*/
publicclass LoginAction
extends ActionSupport{
private String
userName;
private String
password;
private String
msg;
//结果信息属性
/**
*@returnthemsg
*/
public String getMsg() {
returnmsg;
}
/**
*@parammsgthemsgtoset
*/
publicvoid setMsg(String msg) {
this.msg
= msg;
}
/**
*@returntheuserName
*/
public String getUserName() {
returnuserName;
}
/**
*@paramuserNametheuserNametoset
*/
publicvoid setUserName(String userName) {
this.userName
= userName;
}
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
}
/**
*@parampasswordthepasswordtoset
*/
publicvoid setPassword(String password) {
this.password
= password;
}
/**
*处理用户请求的login()方法
*@return结果导航字符串
*@throwsException
*/
public String login()
throws Exception{
if("test".equals(this.userName)
&& "test".equals(this.password)){
msg =
"登录成功,欢迎" +
this.userName;
//获取ActionContext实例。通过它来訪问Servlet
API
ActionContext context = ActionContext.getContext();
//看session中是否已经存放了username。假设存放了:说明已经登录了;
//否则说明是第一次登录成功
if(null
!= context.getSession().get("uName")){
msg =
this.userName +
":你已经登录过了!!!";
}else{
context.getSession().put("uName",
this.userName);
}
returnthis.SUCCESS;
}else{
msg =
"登录失败,username或password错";
returnthis.ERROR;
}
}
public String regist()
throws Exception{
//将username,password加入到数据库中
//...
msg =
"注冊成功。";
returnthis.SUCCESS;
}
}
|
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package
name="my"
extends="struts-default"
namespace="/manage">
<!--
定义处理请求URL为login.action的Action
-->
<action
name="userOpt"
class="org.qiujy.web.struts2.action.LoginAction">
<!--
定义处理结果字符串和资源之间的映射关系 -->
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
</struts>
|
<%@ page
language="java"
pageEncoding="UTF-8"%>
<html>
<head>
<title>用户登录页面</title>
</head>
<body>
<h2>用户入口</h2>
<hr>
<form
action="manage/userOpt!login.action"
method="post">
<table
border="1">
<tr>
<td>username:</td>
<td><input
type="text"
name="userName"/></td>
</tr>
<tr>
<td>password:</td>
<td><input
type="password"
name="password"/></td>
</tr>
<tr>
<td
colspan="2">
<input
type="submit"
value="
确定 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
|
<%@ page
language="java"
pageEncoding="UTF-8"%>
<html>
<head>
<title>用户注冊页面</title>
</head>
<body>
<h2>用户注冊</h2>
<hr>
<form
action="manage/userOpt!regist.action"
method="post">
<table
border="1">
<tr>
<td>username:</td>
<td><input
type="text"
name="userName"/></td>
</tr>
<tr>
<td>password:</td>
<td><input
type="password"
name="password"/></td>
</tr>
<tr>
<td
colspan="2">
<input
type="submit"
value="
注冊 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
|
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package
name="my"
extends="struts-default"
namespace="/manage">
<action
name="userLogin"
class="org.qiujy.web.struts2.action.LoginAction"
method="login">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
<action
name="userRegist"
class="org.qiujy.web.struts2.action.LoginAction"
method="regist">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
</struts>
|
<action
name="user_*"
class="org.qiujy.web.struts2.action.UserAction"
method="{1}">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
|
同一时候method属性值为一个表达式{1}。表示它的值是name属性值中第一个*的值。比如:用户请求URL为user_login.action时。将调用到UserAction类的login方法;用户请求URL为user_regist.action时。将调用到UserAction类的regist方法。
Struts2通过配置逻辑视图名和物理视图资源之间的映射关系,一旦系统收到Action返回的某个逻辑视图名。系统就会把相应的物理视图资源呈现给浏览者。
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirect-action">Logon!input</result>
</global-results>
|
名字
|
说明
|
chain
|
用来处理Action链
|
dispatcher
|
用来转向页面。通常处理JSP,这是默认的结果类型
|
freeMarker
|
处理FreeMarker模板
|
httpHeader
|
用来控制特殊的Http行为
|
redirect
|
重定向到一个URL
|
redirect-action
|
重定向到一个Action
|
stream
|
向浏览器发送InputSream对象,通经常使用来处理文件下载
|
velocity
|
处理Velocity模板
|
xslt
|
处理XML/XLST模板
|
plaintext
|
显示原始文件内容。比如文件源码
|
tiles
|
结合Tile使用
|
private String nextAction;
public String getNextAction() {
return nextAction;
}
|
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-action">${nextAction}</result>
</action>
|
简单的说。就是使用Action实例来封装请求參数和处理结果信息。
前面我们做的演示样例都属于属性驱动模式。
也就是说。使用单独的VO(值对象)来封装请求參数和处理结果信息。
package org.qiujy.domain;
publicclass User {
private String
userName;
private String
password;
/**
*@returntheuserName
*/
public String getUserName() {
returnuserName;
}
/**
*@paramuserNametheuserNametoset
*/
publicvoid setUserName(String userName) {
this.userName
= userName;
}
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
}
/**
*@parampasswordthepasswordtoset
*/
publicvoid setPassword(String password) {
this.password
= password;
}
}
|
package org.qiujy.web.struts2.action;
import org.qiujy.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
publicclass UserAction
extends ActionSupport{
//定义用于封装请求參数的模型对象
private User
user =
new User();
private String
msg;
//结果信息属性
/**
*@returntheuser
*/
public User getUser() {
returnuser;
}
/**
*@paramusertheusertoset
*/
publicvoid setUser(User user) {
this.user
= user;
}
/**
*@returnthemsg
*/
public String getMsg() {
returnmsg;
}
/**
*@parammsgthemsgtoset
*/
publicvoid setMsg(String msg) {
this.msg
= msg;
}
/**
*处理用户请求的login()方法
*@return结果导航字符串
*@throwsException
*/
public String login()
throws Exception{
String userName = user.getUserName();
String password = user.getPassword();
if("test".equals(userName)
&& "test".equals(password)){
msg =
"登录成功,欢迎" + userName;
//获取ActionContext实例,通过它来訪问Servlet
API
ActionContext context = ActionContext.getContext();
//看session中是否已经存放了username,假设存放了:说明已经登录了;否则说明是第一次登录成功
if(null
!= context.getSession().get("uName")){
msg = userName +
":你已经登录过了!!!";
}else{
context.getSession().put("uName", userName);
}
returnthis.SUCCESS;
}else{
msg =
"登录失败。username或password错";
returnthis.ERROR;
}
}
public String regist()
throws Exception{
//将username,password加入到数据库中
//...
msg =
"注冊成功。";
returnthis.SUCCESS;
}
}
|
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package
name="my"
extends="struts-default"
namespace="/manage">
<action
name="userOpt"
class="org.qiujy.web.struts2.action.UserAction">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
</struts>
|
<%@ page
language="java"
pageEncoding="UTF-8"%>
<html>
<head>
<title>用户登录页面</title>
</head>
<body>
<h2>用户入口</h2>
<hr>
<form
action="manage/userOpt!login.action"
method="post">
<table
border="1">
<tr>
<td>username:</td>
<td><input
type="text"
name="user.userName"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password"
name="user.password"/></td>
</tr>
<tr>
<td
colspan="2">
<input
type="submit"
value="
确定 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
|
Struts2提供了一种声明式的异常处理方式。
Struts2也是通过配置的拦截器来实现异常处理机制的。
public String regist()
throws Exception{
//将username。password加入到数据库中
//...
//msg = "注冊成功。";
if(true){
throw new java.sql.SQLException("没有数据库驱动程序");
}
return this.SUCCESS;
}
|
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package
name="my"
extends="struts-default"
namespace="/manage">
<!--
定义全局处理结果 -->
<global-results>
<!--
逻辑名为sql的结果,映射到/exception.jsp页面
-->
<result
name="sql">/exception.jsp</result>
</global-results>
<global-exception-mappings>
<!--
当Action抛出SQLException异常时,转入名为sql的结果
-->
<exception-mapping
exception="java.sql.SQLException" result="sql"/>
</global-exception-mappings>
<action
name="userOpt"
class="org.qiujy.web.struts2.action.UserAction">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
</struts>
|
<%@ page
language="java"
pageEncoding="utf-8"%>
<%@ taglib
uri="/struts-tags"
prefix="s"
%>
<html>
<head>
<title>异常信息</title>
</head>
<body>
<h2>
出现异常啦
</h2>
<hr/>
<h3
style="color:red">
<!--
获得异常对象 -->
<s:property
value="exception.message"/>
</h3>
<br/>
<!--
异常堆栈信息 -->
<s:property
value="exceptionStack"/>
</html>
|
版权声明:本文博主原创文章,博客,未经同意不得转载。