Struts2基本程序演示
Struts2启动配置
<?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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Struts2主配置文件
<?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" />
<!--默认就是UTF-8 所以不配置也行-->
<!--<constant name="struts.i18n.encoding" value="UTF-8"/>--> <package name="default" namespace="/" extends="struts-default">
<action name="index">
<result>/WEB-INF/main.jsp</result>
</action>
</package> <!--struts2qs模块-->
<include file="org/zln/struts2qs/cfg/struts2_struts2qs.xml"/> </struts>
Struts2模块文件配置
<?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.custom.i18n.resources" value="org/zln/struts2qs/resource/messageResource"/> <!--struts2qs模块-->
<package name="struts2qs" namespace="/struts2qs" extends="struts-default">
<!--登录界面-->
<action name="loginUI" class="org.zln.struts2qs.action.LoginAction" method="loginUI">
<result name="success">/WEB-INF/struts2qs/loginForm.jsp</result>
</action>
<!--登录动作-->
<action name="loginDo" class="org.zln.struts2qs.action.LoginAction" method="loginDo">
<result name="success">/WEB-INF/struts2qs/welcome.jsp</result>
<result name="input">/WEB-INF/struts2qs/loginForm.jsp</result>
</action>
<!--获取书籍-->
<action name="getBooks" class="org.zln.struts2qs.action.BookAction" method="getBook">
<result name="success">/WEB-INF/struts2qs/showBooks.jsp</result>
<result name="login">/WEB-INF/struts2qs/loginForm.jsp</result>
</action>
</package> </struts>
Action
package org.zln.struts2qs.action; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils; /**
* Created by sherry on 15-7-8.
*/
public class LoginAction extends ActionSupport {
private String username;
private String password; //登录界面
public String loginUI(){
return SUCCESS;
}
//登录动作
public String loginDo(){
System.out.println("获取参数:" + username + ":" + password);
if ("zln".equals(username)
&&"123".equals(password)){
ActionContext.getContext().getSession().put("user",username);
return SUCCESS;
}else {
return INPUT;
}
} //输入校验 //所有方法的输入校验
@Override
public void validate() {
super.validate();
} //指定方法的输入校验
public void validateLoginDo(){
if (StringUtils.isEmpty(username)){
//对错误信息进行了国际化 也可以直接用文字
addFieldError("username",getText("username.required"));
}
} 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;
}
} /*
Action访问Servlet API
间接访问
static ActionContext getContext()
直接访问
* */
package org.zln.struts2qs.action; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.zln.struts2qs.service.BookService; /**
* Created by sherry on 15-7-8.
*/
public class BookAction extends ActionSupport {
private String[] books; public String getBook(){
String user = (String) ActionContext.getContext().getSession().get("user");
if ("zln".equals(user)){
//实际开发中会从容器中获取依赖对象
BookService bookService = new BookService();
books = bookService.getLeeBooks();
return SUCCESS;
}else {
return LOGIN;
}
} public String[] getBooks() {
return books;
} public void setBooks(String[] books) {
this.books = books;
}
}
jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String home = request.getContextPath();
%>
<html>
<head>
<%--s:text标签输出国际化信息--%>
<title><s:text name="loginPage"/></title>
</head>
<body>
<form action="<%=home%>/struts2qs/loginDo.action" method="post">
<table>
<caption>用户登录</caption>
<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="提交"/>
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
<hr/>
<%--使用Struts2标签 默认提供表单输出提示--%>
<s:form namespace="/struts2qs" action="loginDo" method="POST">
<%--使用标签的 key 输出国际化信息--%>
<s:textfield name="username" key="username"/>
<s:password name="password" label="密 码"/>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String home = request.getContextPath();
%>
<html>
<head>
<%--使用表达式输出国际化信息--%>
<title><s:property value="%{getText('succPage')}"/> </title>
</head>
<body>
欢迎,${sessionScope.user},您已经登录!<br/>
<a href="<%=home%>/struts2qs/getBooks.action">获取书籍列表</a>
</body>
</html>
<%@ page import="com.opensymphony.xwork2.util.ValueStack" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>书籍</title>
</head>
<body>
<table border="1" width="360">
<%
/*Struts2的所有属性参数被封装在此*/
ValueStack valueStack = (ValueStack) request.getAttribute("struts.valueStack");
String[] books = (String[]) valueStack.findValue("books");
for (String book:books){
%>
<tr>
<td>书名:</td>
<td><%=book%></td>
</tr>
<%
}
%>
</table>
<hr/>
<table border="1" width="360">
<caption>书籍</caption>
<s:iterator value="books" status="index">
<s:if test="#index.odd == true">
<tr style="background-color: aqua">
</s:if>
<s:else>
<tr>
</s:else>
<td>书名:</td>
<td><s:property/></td>
</tr>
</s:iterator>
</table>
<hr/>
<a href="${pageContext.request.contextPath}/index.action">返回首页</a>
</body>
</html>
Action访问Servlet API的方法



Struts2基本程序演示的更多相关文章
- C程序演示产生僵死进程的过程
先抄录网上一段对僵死进程的描述: 僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...
- struts2入门程序
struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...
- 制作Android Demo GIF:程序演示效果GIF图录制
[转] 制作Android Demo GIF:程序演示效果GIF图录制 在平时写博客或者分享自己写的程序效果的时候经常需要做成GIF图,以下就是介绍几种常用的GIF录制方法: 一.录制工具 1.( ...
- MapGuide应用程序演示样例——你好,MapGuide!
图 3‑4显示了基于MapGuide的Web应用程序的开发流程,整个开发流程能够分为五个阶段.图中,矩形代表任务,椭圆形被任务使用的或被任务创建的实体,箭头代表数据流. 1) 载入文件类型的数据,配置 ...
- 【UNIX网络编程(三)】TCP客户/server程序演示样例
上一节给出了TCP网络编程的函数.这一节使用那些基本函数编写一个完毕的TCP客户/server程序演示样例. 该样例运行的过程例如以下: 1.客户从标准输入读入一行文本,并写给server. 2.se ...
- html5 canvas程序演示--P1197 [JSOI2008]星球大战
html5 canvas程序演示--P1197 [JSOI2008]星球大战 <!doctype html> <html> <head> <meta char ...
- nginx反向代理tomcat应用,struts2网站程序redirect时导致请求地址错误的解决方法
一个使用struts2的网站在登录页面需要进行redirect跳转,大致如下: <package name="admin" extends="httl-defaul ...
- 【转】 制作Android Demo GIF:程序演示效果GIF图录制
在平时写博客或者分享自己写的程序效果的时候经常需要做成GIF图,以下就是介绍几种常用的GIF录制方法: 一.录制工具 1.(生成动画的工具:Ulead GIF Animator),可以讲单独的图片生成 ...
- 经典网页设计:20个华丽的 iPhone 应用程序演示网站
一个物品销售很好,重要的原因之一是它的包装,因为这是最重要的细节,可以把一个人转变成购买者.一个好的包装设计和良好的表现比产品本身更重要,因此被分配了大量的金钱和资源,以创造伟大的东西. 因此,为了销 ...
随机推荐
- 使用Mac的过程中的一些小操作
前言:使用Mac的过程中的一些小操作 查看Mac系统是32位还是64位: 方法1: 点击左上角的苹果按钮->关于本机->概览->系统报告->软件->偏好设置面板:右侧有提 ...
- SpringBoot学习13:springboot异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)
问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...
- .Net Core爬虫爬取妹子网图片
现在网上大把的Python的爬虫教程,很少看见有用C#写的,正好新出的.Net Core可以很方便的部署到Linux上,就用妹子图做示范写个小爬虫 在C#下有个很方便的类库 HtmlAgilityPa ...
- IDEA 编辑框光标闪烁
依次打开如下菜单: File -> Settings -> Editor -> General -> Appearance -> 选中 Caret blinking (m ...
- php-5.6.26源代码 - opcode列表
文件 php-5.6.26/Zend/zend_vm_opcodes.h #ifndef ZEND_VM_OPCODES_H #define ZEND_VM_OPCODES_H BEGIN_EXTER ...
- LInux操作随手笔记
一.find 的用法 实例 find / -name test.txt 就可以找到这个文件的路径(如果存在). 二.学用vi编辑器,学用rz往linux服务器上面上传文件 linux中rz 和 sz ...
- Linux编译移植Qt5的环境_Xillinx的ZYNQ平台
Linux编译Qt环境 2017年的十一假期,足不出户,一个人在教研室里面搞Qt的移植.我手里面有Samsung的CortexA8,Samsung的 CortexA53还有Ti的Sitara系列的AM ...
- Fragment Touch事件泄露
当Fragment的栈里面有几个fragment的时候,这个时候如果是几个fragment状态是hide,当你触摸当前fragment的时候,下层的fragment的事件被触发,这是由于Touch事件 ...
- js柱状图
<!doctype html><html lang="en"><head><script type="text/javascri ...
- 2,MongoDB之增删改查及pymongo的使用
本章我们来学习一下关于 MongoDB的增删改查 一.MongoDB操作 之 原生ORM,根本不存在SQL语句 创建数据库:这里和一般的关系型数据库一样,都要先建立一个自己的数据库空间 是的,Mong ...