Action和JSP之間的通訊

<%@ 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>Struts2 HelloWorld示例</title>
	
  </head>
  
  <body>
    <h3>Struts2 HelloWorld示例</h3>
    <hr>
    <form action="login3.action" method="post">
    	用戶名:<input type="text" name="userName"><br>
    	密    碼:<input type="password" name="password"><br>
    	<input type="submit" value="登陸">&nbsp;&nbsp;
    	<input type="reset" value="重置">
    </form>
    
    <hr/>
   	<a href="stu!add.action">增長</a><br/>
   	<a href="stu!delete.action">刪除</a><br/>
   	<a href="stu!update.action">修改</a><br/>
   	<a href="stu!find.action">查詢</a><br/>
  </body>
</html>
<?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">

<!-- step3:struct2 的核心配置文件 -->
<!-- step4:添加項目所需的文件 -->
<struts>
	<!-- 請求消息的編碼方式  默認的編碼爲UTF-8 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<!-- 指定被struts2處理的請求後綴類型。多個用逗號隔開 -->
	<constant name="struts.action.extension" value="action,do,go,zhangsan,lisi"></constant>
    <!-- 默認值爲false(生產環境下使用),開發階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <!-- 是否使用struts的開發模式。開發模式會有更多的調試信息。默認值爲false(生產環境下使用),開發階段最好打開 
     -->
    <constant name="struts.devMode" value="false"></constant>
    <!-- 啓用Action的name是否支持斜線(/) -->
    <constant name="struts.enable.SlashesInActionNames" value="true"></constant>
   	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
   	
   	
    <package name="default" namespace="/" extends="struts-default">
    
    	<!-- Action 在Struts2中Action是用來處理請求業務的 -->
		<action name="login" class="derun.action.LoginAction">
			<result name="success">/success.jsp</result>
			<result name="fail">/fail.jsp</result>
		</action>
		
		<action name="login2" class="derun.action.LoginAction2">
			<result name="success">/success.jsp</result>
			<result name="error">/fail.jsp</result>
		</action>
		
		<action name="login3" class="derun.action.LoginAction3">
			<result name="success">/success.jsp</result>
			<result name="error">/fail.jsp</result>
		</action>
		
		<action name="stu" class="derun.action.StudentAction">
			<result name="add">/add.jsp</result>
			<result name="delete">/delete.jsp</result>
			<result name="update">/update.jsp</result>
			<result name="find">/find.jsp</result>
		</action>
    </package> 


</struts>
package derun.action;

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

import derun.model.Student;

public class StudentAction extends ActionSupport {

	private Student stu;
	public String add(){
		System.out.println("添加信息:"+stu.getStuName()+"--->年齡:"+stu.getAge());
		return "add";
	}
	public String delete(){
		System.out.println("刪除信息");
		return "delete";
	}
	public String update(){
		System.out.println("更新信息");
		return "update";
	}
	public String find(){
		System.out.println("查找信息");
		//ActionContext
		ActionContext context=ActionContext.getContext();
		context.put("name", "zhangsan");//request.setAttribute(key,value);
		context.getSession().put("age", 20);//session.setAttribute();
		context.getApplication().put("address", "beijing");//ServletContext.setAttribute();
		
		return "find";
	}
	public void setStu(Student stu) {
		this.stu = stu;
	}
	
}
<%@ 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 'find.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>
    <h3>查詢頁面</h3>
    <hr>
    ${name }
    ${age }
  </body>
</html>
相關文章
相關標籤/搜索