Struts2 XML配置详解
2019独角兽企业重金招聘Python工程师标准>>>
<! 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;
/**
* @author qiujy
* @version 1.0
*/
public class LoginAction extends ActionSupport {
private String userName ;
private String password ;
private String msg ; // 结果信息属性
/**
* @return the msg
*/
public String getMsg() {
return msg ;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this . msg = msg;
}
/**
* @return the userName
*/
public String getUserName() {
return userName ;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this . userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password ;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this . password = password;
}
/**
* 处理用户请求的 excute() 方法
* @return 结果导航字符串
* @throws Exception
*/
public String execute() throws Exception{
if ( "test" .equals( this . userName ) &&
"test" .equals( this . password )){
msg = " 登录成功,欢迎 " + this . userName ;
return this . SUCCESS ;
} else {
msg = " 登录失败,用户名或密码错 " ;
return this . 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 );
}
return this . SUCCESS ;
} else {
msg = " 登录失败,用户名或密码错 " ;
return this . ERROR ;
}
}
|
<form method="post" action="userOpt!login.action"> |
package org.qiujy.web.struts2.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author qiujy
* @version 1.0
*/
public class LoginAction extends ActionSupport{
private String userName ;
private String password ;
private String msg ; // 结果信息属性
/**
* @return the msg
*/
public String getMsg() {
return msg ;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this . msg = msg;
}
/**
* @return the userName
*/
public String getUserName() {
return userName ;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this . userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password ;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this . password = password;
}
/**
* 处理用户请求的 login() 方法
* @return 结果导航字符串
* @throws Exception
*/
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 );
}
return this . SUCCESS ;
} else {
msg = " 登录失败,用户名或密码错 " ;
return this . ERROR ;
}
}
public String regist() throws Exception{
// 将用户名,密码添加到数据库中
//...
msg = " 注册成功。 " ;
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" >
<!-- 定义处理请求 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 >
|
<%@ 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;
}
|
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-action">${nextAction}</result>
</action>
|
package org.qiujy.domain;
public class User {
private String userName ;
private String password ;
/**
* @return the userName
*/
public String getUserName() {
return userName ;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this . userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password ;
}
/**
* @param password the password to set
*/
public void 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;
public class UserAction extends ActionSupport{
// 定义用于封装请求参数的模型对象
private User user = new User();
private String msg ; // 结果信息属性
/**
* @return the user
*/
public User getUser() {
return user ;
}
/**
* @param user the user to set
*/
public void setUser(User user) {
this . user = user;
}
/**
* @return the msg
*/
public String getMsg() {
return msg ;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this . msg = msg;
}
/**
* 处理用户请求的 login() 方法
* @return 结果导航字符串
* @throws Exception
*/
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);
}
return this . SUCCESS ;
} else {
msg = " 登录失败,用户名或密码错 " ;
return this . ERROR ;
}
}
public String regist() throws Exception{
// 将用户名,密码添加到数据库中
//...
msg = " 注册成功。 " ;
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" >
< 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;
}
|
<! 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 >
|
转载于:https://my.oschina.net/softwarechina/blog/135951
Struts2 XML配置详解相关推荐
- mybatis 同名方法_MyBatis(四):xml配置详解
目录 1.我们将 数据库的配置语句写在 db.properties 文件中 2.在 mybatis-configuration.xml 中加载db.properties文件并读取 通过源码我们可以分析 ...
- Java web之web.xml配置详解
什么是web.xml web.xml是web项目的配置文件,一般的web工程都会用到web.xml来配置,方便大型开发.web.xml主要用来配置Filter,Listener,Servlet等.但是 ...
- SpringBoot—整合log4j2入门和log4j2.xml配置详解
关注微信公众号:CodingTechWork,一起学习进步. 引言 对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...
- JavaWeb web.xml配置详解
参考: XML 教程 Java web之web.xml配置详解 Javaweb三大组件是:Servlet,Filter,Listener. 1.Servlet Servlet作为中转处理的容器,连接了 ...
- Maven的settings.xml配置详解
Maven的settings.xml配置详解 1 基本介绍 maven的两大配置文件:settings.xml和pom.xml.其中settings.xml是maven的全局配置文件,pom.xml则 ...
- Ehcache 中ehcache.xml 配置详解和示例
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存 ...
- web.xml配置详解
往者不谏 来者可追 写作是最好的社交 新随笔 订阅 web.xml配置详解 Web.xml常用元素<web-app><display-name></display-nam ...
- Maven settings.xml配置详解
首先:Maven中央仓库的搜索全部公共jar包的地址是,http://search.maven.org/ ===Maven基础-默认中央仓库============================== ...
- 160329(二)、web.xml配置详解
1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...
最新文章
- Swift开发:仿Clear手势操作(拖拽、划动、捏合)UITableView
- CSS设置图片的重复
- html div剩下高度设置,使div填充剩余屏幕空间的高度
- 西电计算机应用基础 一,15秋西电《计算机应用基础(一)》在线作业答案解析.doc...
- 计算机视觉论文-2021-06-16
- 垃圾回收算法与实现系列-学习GC之前的准备工作
- 大数据分析有几种方法
- mysql技术blog_Mysql技术 - 包子博客 _ 关注互联网前端、开发、SEO、移动互联网应用技术...
- HDU 2009 整除的尾数 题解
- 如何让音乐软件的歌词在touch bar上面显示?
- 服务器解决了什么问题、状态同步和帧同步
- python123测试4四位玫瑰数_心理测试:4朵玫瑰,你最喜欢哪一朵?测测你这辈子会有多少情债...
- 基于微信小程序的童装(服装)商城的设计与实现
- python 读取csv某一列_Python 读取csv的某列
- 看门狗watchdog的理解
- Linux下Mysql数据库
- 射影几何----渐近线方程公式
- 格式化字符串漏洞及利用_萌新食用
- centos7系统root分区扩容
- 服务器磁盘阵列-RAID配置