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 应用程序演示网站
一个物品销售很好,重要的原因之一是它的包装,因为这是最重要的细节,可以把一个人转变成购买者.一个好的包装设计和良好的表现比产品本身更重要,因此被分配了大量的金钱和资源,以创造伟大的东西. 因此,为了销 ...
随机推荐
- windows 编译安卓iconv 库
由于NDK r15后,谷歌要统一将来的设备都要支持64位,而iconv只支持32位,后续的ndk都会去除iconv的支持,所以只能在iconv的官网下载源码编译库文件使用, 下载地址:https:// ...
- 在 publicId 和 systemId 之间需要有空格。
spring applicationContext_datasource.xml中约束错误 org.springframework.beans.factory.xml.XmlBeanDefiniti ...
- Spring框架基础2
Spring框架基础2 测试Spring的AOP思想和注解的使用 导包(在前面的基础上添加) SpringAOP名词解释 AOP编程思想:横向重复代码,纵向抽取:就是说多个地方重复的代码可以抽取出来公 ...
- Spring Cloud 入门Eureka -Consumer服务消费(Ribbon)(二)
前面一篇介绍了LoadBalancerClient来实现负载均衡, 这里介绍Spring cloud ribbon 1.ribbon Spring Cloud Ribbon 是一个基于Http和TCP ...
- MARK 一条关于Linux 运维方面个人向收藏网址
吴钧泽博客 https://wujunze.com/archives.html Linux运维笔记 https://blog.linuxeye.cn/ Linux中文网 http://www.ppze ...
- python中字符串编码方式小结
Python2中字符串的类型有两种:str和unicode,其中unicode是统一编码方式,它使得字符跟二进制是一一对应的,因此所有其他编码的encode都从unicode开始,而其他编码方式按照相 ...
- 百度MIP校验错误整理与解决方法
MIP校验工具地址: https://www.mipengine.org/validator/validate 1.强制性标签缺失或错误 错误提示:line 1,col 1: 强制性标签'<sc ...
- PHP Socket服务器搭建和测试
1.socket服务器搭建思路 1) 目的:理解socket服务器工作机制 2) 思路:创建socket -> 把socket加入连接池 -> 处理接收信息 -> 握手动作 -> ...
- SSO 单点登录总结(PHP)
本篇文章根据个人理解的知识整理汇总,如有不足之处,请大家多多指正. 单点登录(SSO--Single Sign On)的应用是很普遍的,尤其在大型网站系统中,比如百度,登录百度账号和,再转到百度经验. ...
- C语言字符篇(五)内存函数
memcpy不可以把目的地址写成本身 但是memmove可以,因为它是先保存到临时空间 #include <string.h> void *memcpy(void *dest, con ...