Struts2 XML配置详解
<!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>
<struts>
......
<constant name="struts.custom.i18n.resources" value="messages"/>
</struts>
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 = "登录失败,用户名或密码错";
returnthis.ERROR;
}
}
}
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中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功
if(null != context.getSession().get("uName")){
msg = this.userName + ":你已经登录过了!!!";
}else{
context.getSession().put("uName", this.userName);
} returnthis.SUCCESS;
}else{
msg = "登录失败,用户名或密码错";
returnthis.ERROR;
}
}
<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中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功
if(null != context.getSession().get("uName")){
msg = this.userName + ":你已经登录过了!!!";
}else{
context.getSession().put("uName", this.userName);
} returnthis.SUCCESS;
}else{
msg = "登录失败,用户名或密码错";
returnthis.ERROR;
}
} public String regist() throws Exception{
//将用户名,密码添加到数据库中
//...
msg = "注册成功。";
returnthis.SUCCESS;
}
}
2. struts.xml文件:没有什么变化,跟以前一样配置
<!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>用户名:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value=" 确定 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
regist.jsp
<%@ 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>用户名:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密码:</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>
<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;
}
在strutx.xml配置文件中,我们可以使用${nextAction}来引用到Action中的属性,通过${nextAction}表示的内容来动态的返回结果,例如:
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-action">${nextAction}</result>
</action>
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;
}
}
2. 业务控制器:UserAction.java
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中是否已经存放了用户名,如果存放了:说明已经登录了;否则说明是第一次登录成功
if(null != context.getSession().get("uName")){
msg = userName + ":你已经登录过了!!!";
}else{
context.getSession().put("uName", userName);
} returnthis.SUCCESS;
}else{
msg = "登录失败,用户名或密码错";
returnthis.ERROR;
}
} public String regist() throws Exception{
//将用户名,密码添加到数据库中
//...
msg = "注册成功。";
returnthis.SUCCESS;
}
}
3. 配置文件:struts.xml
<!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>用户名:</td>
<td><input type="text" name="user.userName"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value=" 确定 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
public String regist() throws Exception{
//将用户名,密码添加到数据库中
//...
//msg = "注册成功。";
if(true){
throw new java.sql.SQLException("没有数据库驱动程序");
} return this.SUCCESS;
}
2) 修改struts.xml文件:
<!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>
3) 新增一页面:exception.jsp
<%@ 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>
4) 运行regist.jsp进行调试:
Struts2 XML配置详解的更多相关文章
- java web.xml配置详解(转)
源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...
- struts2基本配置详解2
接上篇struts2基本配置详解,还有一些配置没有讲到,下面将继续. struts.xml <package name="com.amos.web.action" names ...
- Spring 入门 web.xml配置详解
Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...
- web.xml配置详解之listener
web.xml配置详解之listener 定义 <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...
- tomcat中server.xml配置详解(转载)(一)
转载自:https://www.cnblogs.com/starhu/p/5599773.html tomcat中server.xml配置详解 Tomcat Server的结构图如下:(该文件描述了如 ...
- Web.xml配置详解(转)
Web.xml配置详解 Posted on 2010-09-02 14:09 chinaifne 阅读(295105) 评论(16) 编辑 收藏 1 定义头和根元素 部署描述符文件就像所有XML文件一 ...
- SpringBoot—整合log4j2入门和log4j2.xml配置详解
关注微信公众号:CodingTechWork,一起学习进步. 引言 对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...
- Tomcat中的Server.xml配置详解
Tomcat中的Server.xml配置详解 Tomcat Server的结构图如下: 该文件描述了如何启动Tomcat Server <Server> <Listener /> ...
- Log4j2详解——XML配置详解
Log4j2详解--XML配置详解 找到了个很详细的文章链接 https://www.jianshu.com/p/bfc182ee33db
随机推荐
- QT QString 很全的使用 (转)
QString, QByteArray, 和 QVariant这三个类和容器有许多相同之处,并且在一些情况下可以被当作特殊的容器. 同样,像容器,这些类使用隐式共享来优化内存和速度. 我们将从QStr ...
- Ninject使用介绍
#region 第二种写法 /// <summary> /// using(IKernel tKernel=new StandardKernel(new PeoKernelServer() ...
- 打开页面自动打开QQ的javascript代码
今天浏览个网站,发现我的QQ突然自动启动了,起初还以为中病毒了,后来找了半天无果,反而发现了几个无需启动的系统服务进程,遂管之. 后来打开网站的页面的源代码,发现一段javascript脚本,才知道我 ...
- Android从零开始--安装
1.下载安装eclipse.adt和Android sdk(以前一直以为Android使用的sdk也是java jdk呢,呵呵) 2.都安装完成后配置eclipse的Android的环境,将Andro ...
- OAF_开发系列01_实现OAF资料主从关系Master-Detail联动(案例)
2014-06-02 Created By BaoXinjian
- LSOF 安装与使用
linux上安装: tar zxvf lsof_4.76.tar.gz cd lsof_4.76 ls 00.README.FIRST_4.76 lsof_4.76_src.tar.gz README ...
- 如何在Delphi里面查看程序的汇编代码?
开发工具:Delphi 10 Update2 1,在源代码中设置至少一个断点,目的让我们进入调试模式. 2,启动调试,当程序进入调试模式,停留在我们设定的断点处时候,使用快捷键"CTRL+A ...
- C 指针疑虑
uint16 *a; a=(uint16 *)b; 将变量b强制转换为Uint16类型的指针,然后赋值给Uint16类型的指针变量a. 如: uint8 WriteLpa(uint8 *buffer, ...
- matlab工具箱之人眼检测+meanshift跟踪算法--人眼跟踪
Viola-Jones 人眼检测算法+meanshift跟踪算法 这次的代码是对视频中的人眼部分进行检测加跟踪,检测用的是matlab自带的人眼检测工具箱 下面是matlab官网介绍这个算法的一些东西 ...
- 开启win7系统关闭日志分析
笔记本突然没有任何征兆的自动重启了,想起之前曾经在网上看过通过系统日志查看一下是哪个东东搞的鬼,于是开始上网搜索,发现默认情况下是关机的信息记录的很少,不过可以开启审计功能来记录这个捣鬼的程序.于是就 ...