struts2 OGNL

java类

package cn.hb.si;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
	
	ActionContext context = ActionContext.getContext();

	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);

	
	public String add() {
		//System.out.println(request);
		request.setAttribute("path", "update");
		return "add";

	}

	public String update() {
		return "update";

	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("执行Action");
		// 返回string 可以理解为视图的路径
		return SUCCESS;
	}


}

struts2.xml

<?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>
	
	<!-- 引入其他的配置文件 可以添加多个,也可以添加包名-->
	<include file="hello.xml"></include>
	<!-- 配置编码方式 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  	<!--  配置后缀
  		<init-param>
  	 	<param-name>struts.action.extension</param-name>
  	 	<param-value>do</param-value>
  	 </init-param>
  	 -->
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

hello.xml

<?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>

	<package name="default" namespace="/" extends="struts-default">
		<default-action-ref name="index"></default-action-ref>
		<action name="index">
			<result>/error.jsp</result>
		</action>

		<action name="*_*_*" method="{2}" class="cn.hb.{3}.{1}Action">
			<result>/result.jsp</result>
			<result name="add">
				<param name="location">/${#request.path}.jsp</param>
			</result>
			<result name="update">/{2}.jsp</result>
			<result name="error">/error.jsp</result>
		</action>

		<action name="LoginAction" method="login" class="cn.hb.si.LoginAction">
			<result>/success.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
	</package>
</struts>

访问地址:http://localhost:8080/Struts2_1zixue/Hello_add_si.action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值