深入学习Struts2
<!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;
}
}
|
<!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"></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;
}
}
|
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;
}
}
|
<!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>
|
深入学习Struts2的更多相关文章
- 学习struts2
有部分内容转载牛人的博客: http://blog.csdn.net/hudie1234567/article/details/6730481 http://blog.csdn.net/lishuan ...
- 学习Struts2的个人疑惑及问题解决
刚开始学习SSH框架中Struts2时,个人疑惑以及一些问题总结一下. 1.package节点namespace属性值决定访问路径问题 namespace不写或写namespace=&qu ...
- 学习struts2及MVC
1.MVC模式基础 1.1.MVC模式简介 MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而达到更好的开发和维护效率.在MVC模式中 ...
- 学习Struts2的第一个应用步骤
---恢复内容开始--- 一.在官网 http://struts.apache.org/下载struts-2.3.31-all 1. 打开struts-2.3.31-all,将struts-2.3. ...
- 框架学习Struts2之HelloWord
一.概述 Struts2是基于MVC设计模式的web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts作为控制器(Controller)来建立模型与视图之间的数据交互.Str ...
- SSH框架学习------struts2前后台传值(二)
struts2的Action类一般都会继承(extends)ActionSupport类(Action类,即实现action的类)1.前传后:jsp里<input type="text ...
- SSH框架学习------struts2(一)
1.总的目录 2.所有程序 1)index.jsp很简单 <%@ page language="java" contentType="text/html; char ...
- Java——Struts2 crud 简单实例(学习struts2和ssh) 用Myeclipse实现
1.new web project 2.给新建的web项目添加struts2支持 3.项目结构中有了struts.xml和struts2核心库 4.编码 4.1项目结构图 4.2源代码: (1)DbU ...
- 学习Struts2经验总结
一.struts 访问路径问题 1) Struts2的思想:主要围着“action”转,只要找到“action”它就知道自己该干嘛了. 首先配置struts.xml ,我们可以明白的看到,action ...
随机推荐
- 【zoj2562】反素数
题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最小的一个.(1 <= n <= 10^16) 题目:http://acm.hust.edu. ...
- PLSQL:plsql中文乱码,显示问号
PLSQL执行sql语句,不识别中文,输出的中文标题显示成问号???? 1. 登陆plsql,执行sql语句,输出的中文标题显示成问号????:条件包含中文,则无数据输出
- PushBackInputStream与PushBackInputStreamReader的用法
举个例子:获取XX内容 PushBackInputStream pb=new PushBackInputStream(in,4);//4制定缓冲区大小 byte[] buf=new byte[4]; ...
- 重温《js权威指南》 第2-3章
第二章 语法结构 2.1 js区分大小写,html不区分大小写 2.5 注意分号,如果没有分号,解释器会试图解析js,并在不能解析的地方加分号 第三章 值和变量 ...
- jquery.flip插件翻转效果
首先去网站http://lab.smashup.it/flip/下载插件 简单应用: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- http://blog.csdn.net/sd0902/article/details/8395677
http://blog.csdn.net/sd0902/article/details/8395677
- 在CentOS 7中安装与配置Tomcat-8方法
安装前提 在CentOS 7中安装与配置JDK8 安装tomcat apache-tomcat-8.0.14.tar.gz文件上传到/usr/local中执行以下操作: [root@localhos ...
- jboss内存管理
jboss内存查看 1. 用浏览器打开网址:http://IP:port/jmx-console/ 2. 找到 jboss.system 一节,或者在 filter 文本框中输入 jboss.syst ...
- lua简化cocos2dx的Action动画序列
情景 今天写代码时,又要写一个很常见的动画,就是变大变小模拟那个弹性的赶脚,很常用但写起来挺麻烦,封装一下用起来就简单多了. 当然我也知道有缓动动画(EaseAction)可以实现反弹效果,但这不是重 ...
- 测试 float
关于blog上对 float的许多讨论,这个函数不对,那个运算出错. 其实原因不在这里,根源是计算机没有向你保证你看到的就是真实的,都是一厢情愿而已. 废话不说.下面是测试,一看就明白.再不明白的看看 ...