struts2的知识点

下载struts2的文档

foward 和 redirected的区别

一次request只有一个值栈,就是说chain,共享值栈

jsp页面得到参数
<s:property value="#parameters.t"/>
${param.t }



在工作目录中添加源码
在工作目录中添加doc(方便在Myeclipse中直接看它的API文档)

2.1.6中这个有BUG,显示中文的时候是乱码,只能在2.1.7中修复
<constant name="struts.i18n.encoding" value="UTF-8" />

让struts2处于开发模式,这样,改了action也可以不用重启服务即可使用
在struts.xml中添加<constant name="struts2.devMode" value="true"/>

<package name="front" extends="struts-default" namespace="/front">
	<action name="index">
		<result name="success">/hello.jsp</result>
	</action>
</package>

默认namespace为"" 如果加了"/aaa",那么对应的action也得写成 aaa/front.action


struts1中,action只有一个
struts2中,action每次访问都会new一个新的

当struts.xml中某一个action没有对应的class时,action默认的action为actionSupport中的execute()

struts2中的路径问题是根据action的路径而不是jsp路径来确定的,所以劲量不要使用相对路径
虽然可以使用redirect方式解决,但redirect方式并非必要
解决方法非常简单,统一使用相对路径(在jsp页面中用request.getContextRoot方式得到webapp路径)

action有3种接受参数的方法

在action中定义jsp页面中的参数名,然后get()set()

直接用对象传参(user.name=aaa&user.age=8)



<s:debug/>
包括两个contents

value stack contents  值栈,action的属性会放入此中,包括errors
stack contents 栈上下文





=============================================================================

在struts2中,如果用struts.xml来配置action,那么一般使用动态方法调用(DMI)和通配符匹配
调用action的时候,用action!method来调

struts.xml:
<package name="default" extends="struts-default" namespace="/">
		<action name="test1action" class="com.test.action.Test1Action">
			<result>
				/hello.jsp
			</result>
		</action>		
	</package>

action:

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.entity.UserVO;

public class Test1Action extends ActionSupport {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7515077332560688530L;

	private UserVO userVO;
	
	public UserVO getUserVO() {
		return userVO;
	}

	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}



	public String test1() {
		if (userVO.getName() != null && !userVO.getName().equals("admin")) {
			this.addFieldError("name", "name is error");
		}
		System.out.println(userVO.getName() + "  " + userVO.getAge());
		return SUCCESS;
	}
}



jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <!-- <base href="<%=basePath%>"> -->
    
    <title>My JSP 'index.jsp' starting page</title>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    hello
    <form action="test1action!test1" method="post">
    	<input type="text" name="userVO.name">
    	<br/>
    	<input type="text" name="userVO.age">
    	<br/>
    	<input type="submit" value="提交">
    </form>
    ${userVO.name } + ${userVO.age }
    <br/>
    <s:property value="#request.userVO.name"/>
    <br/>
    <s:property value="#attr.userVO.name"/>
    <br/>
    ============================================
    <br/>
    <s:property value="errors.name[0]" />
  </body>
</html>


可以用el表达式来取值${},也可以用<s:property value="#request.***" />

<s:property value="#session.***" />
<s:property value="#application.***" />
当然,也可以使用<s:property value="#attr.***" />但不建议使用,因为最好精确知道值在哪个域中。

我个人使用el表达式,因为get()set()后,在jsp页面上即可以得到request的值



======================================================================================



默认action(如果找不到某个action时,走此action):
<default-action-ref name="aaa"></default-action-ref>
<action name="aaa">
	<result>/aaa.jsp</result>
</action>


当使用chain时,如果一个action在另外一个包中,那么使用参数,把另一个包的名放进去即可(看文档)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值