Struts2

来源:互联网 发布:阿里云邮箱个性签名 编辑:程序博客网 时间:2024/06/08 06:39

Struts2


2.1.6中文bug:

web.xml

        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>        <!-- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> -->
struts.xml

<constant name="struts.i18n.encodeing" value="UTF-8"/>

访问WEB元素

Map request = (Map)ActionContext.getContext().get("request");Map session = ActionContext.getContext().getSession();Map spplication = ActionContext.getContext().getApplication();request.put("r1", "rrr的v公司的发布");session.put("s1", "sss的v公司的发布");spplication.put("a1", "aaaa的v公司的发布");
    <s:property value="#request.r1"/> | <%=request.getAttribute("r1")%><br>    <s:property value="#session.s1"/> | <%=session.getAttribute("s1")%><br>    <s:property value="#application.a1"/> | <%=application.getAttribute("a1")%><br>

模块包含:

<include file="web1.xml"></include>

默认Action:

<default-action-ref name="index"></default-action-ref>

结果类型:

dispatcher(默认)   服务器跳转页面

redirect 重定向页面

chain 服务器跳转Action

redirectAction 重定向到Action

全局结果集

<global-results><result name="mainpage">/main.jsp</result></global-results>

带参数结果集

<result name="success" type="redirect">/hello.jsp?t=${type}</result>

FieldError

public String execute() throws Exception {if("admin".equals(person.getName())){return SUCCESS;}this.addFieldError("err", "用户名错误");return "error";} 
<s:fielderror fieldName="err"></s:fielderror>


基本框架:

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   <display-name>Struts Blank</display-name>    <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>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>      <constant name="struts.devMode" value="true" />    <package name="default" namespace="/" extends="struts-default">            <action name="user" class="com.java.UserAction">           <result name="success">/hello.jsp</result>           <result name="error">/error.jsp</result>        </action>            </package>    <!-- Add packages here --></struts>
Person.java

package com.java;public class Person {private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}

index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <h1>index</h1><form action="user">姓名:<input type="text" name="person.name"><br>年龄:<input type="text" name="person.age"><br><input type="submit" value="提交"></form>  </body></html>
UserAction.java

package com.java;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class UserAction extends ActionSupport implements ModelDriven<Person>{private Person person = new Person();public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}public String execute() throws Exception {System.out.println(person.getName());return SUCCESS;} public Person getModel() {return person;} }
hello.jsp

  <body>    <h1>hello  success!!!</h1>    1${person}<br>    2${person.name}<br>    3${person.age}<br>  </body>

OGNL表达式:

若无法执行方法则应在struts.xml添加 <constant name="struts.ognl.allowStaticMethodAccess" value="true" />

Abc.java

public class Abc {public static String dd = "死了啊";public static String aaa(){System.out.println("aaaaaaaaaaaaa执行了");return "我知行楼";}}

访问指定类的静态属性:

    <h1>hello  success!!! !!</h1>   <s:property value="@com.java.Abc@dd"/><!-- 访问指定类的静态属性 -->   <s:property value="@com.java.Abc@aaa()"/><!-- 访问指定类的静态方法 -->   <s:property value="@@max(2,3)"/><!-- 访问Mathg类中静态方法 -->   <s:property value="new com.java.Abc()"/><!-- new对象 -->   <s:property value="[0].person"/><!-- Value Stack值栈 -->      <s:property value="users"/><!-- 访问List -->   <s:property value="users[1]"/><!-- 访问List某个元素 -->   <s:property value="users.{age}"/><!-- 访问List某属性集合 -->   <s:property value="users.{age}[0]"/><!-- 访问List某属性集合中指定值 -->   <s:property value="dogs"/><!-- 访问Set -->   <s:property value="dogs[1]"/><!-- 访问Set某个属性 -->   <s:property value="dogMap"/><!-- 访问Map -->   <s:property value="dogMap.dog101"/><!-- 访问Map某个元素 -->   <s:property value="dogMap.keys"/><!-- 访问Map中所有key -->   <s:property value="dogMap.values"/><!-- 访问Map中所有的value -->   <s:property value="dogMap.size()"/><!-- 访问容器大小 -->      <s:property value="users.{?#this.age==1}.{age}"/><!-- 投影过滤 -->   <s:property value="users.{^#this.age==1}.{age}"/><!-- 投影(取第一个) -->   <s:property value="users.{$#this.age==1}.{age}"/><!-- 投影(取结尾)-->

Struts标签:

   <s:property value="'username'"/><br>   <s:property value="username" default="管理员"/><br>   <s:property value="'<hr>'" escape="false"/><br>   <s:set var="name" value="1223" scope="page"></s:set><br>    <s:bean name="com.java.Person" var="myDog">    <s:param name="name" value="'dagou'"></s:param>    </s:bean>    <s:include value="/1.html"></s:include><br>        <s:if test="2>3">正确</s:if>    <s:elseif test="2>1">这才正确</s:elseif><br>        <s:iterator value="{'aaa','bbb','ccc'}" status="status">    ${status.count}遍历过的总数    ${status.index}遍历当前的索引    ${status.even}当前是偶数?    ${status.odd}当前是奇数?    ${status.first}第一个元素?    ${status.last}最后一个元素?    <br>    </s:iterator>        <s:iterator value="#{1:'a',2:'b',3:'c'}">    ${key }    </s:iterator>        <s:iterator value="#{1:'a',2:'b',3:'c'}" var="x">    ${x.value}    </s:iterator>








0 0
原创粉丝点击