Struts2

struts2的前后台交互

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<a href="product-input">Product Input</a>
	
	<br><br>
	
	<a href="test.action">Test</a>
</body>
</html>

struts.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: 包. struts2 使用 package 来组织模块. 
		name 属性: 必须. 用于其它的包应用当前包. 
		extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
		         struts-default 这个包在 struts-default.xml 文件中定义.
		namespace 可选, 如果它没有给出, 则以 / 为默认值. 
		                        若 namespace 有一个非默认值, 则要想调用这个包里的Action, 
		                        就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里
		                        
		          http://localhost:8080/contextPath/namespace/actionName.action
	-->
    <package name="helloWorld" extends="struts-default">
    	
    	<!-- 
    		配置一个 action: 一个 struts2 的请求就是一个 action 
    		name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
    		class 的默认值为: com.opensymphony.xwork2.ActionSupport
    		method 的默认值为: execute
    		result: 结果. 
    	-->
    	<!-- <action name="product-input" 
    		class="com.opensymphony.xwork2.ActionSupport"
    		method="execute"> -->
			<action name="product-input" 
    		class="com.atguigu.struts2.helloworld.Product"
    		method="halou">
    		<!--  
    			result: 结果. 表示 action 方法执行后可能返回的一个结果. 所以一个 action 节点可能会有多个 result 子节点.
    			多个 result 子节点使用 name 来区分
    			name: 标识一个 result. 和 action 方法的返回值对应. 默认值为 success
    			type: 表示结果的类型. 默认值为 dispatcher(转发到结果.)
    		-->
    		<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
    	</action>
    	<action name="product-save" class="com.atguigu.struts2.helloworld.Product"
    		method="save">
    		<result name="details">/WEB-INF/pages/details.jsp</result>	
    	</action>
    	
    	<action name="test" class="com.atguigu.struts2.helloworld.Product" method="test">
    		<result>/index.jsp</result>
    	</action>
    	
    </package>

</struts>

Product.java
package com.atguigu.struts2.helloworld;

public class Product {
	private Integer productId;
	private String productName;
	private String productDesc;
	
	private double productPrice;

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductDesc() {
		return productDesc;
	}

	public void setProductDesc(String productDesc) {
		this.productDesc = productDesc;
	}

	public double getProductPrice() {
		return productPrice;
	}

	public void setProductPrice(double productPrice) {
		this.productPrice = productPrice;
	}

	@Override
	public String toString() {
		return "Product [productId=" + productId + ", productName="
				+ productName + ", productDesc=" + productDesc
				+ ", productPrice=" + productPrice + "]";
	}
	
	public String save(){
		System.out.println("save: " + this);
		return "details";
	}
	
	public String test(){
		System.out.println("test");
		return "success";
	}
	
	public Product() {
		System.out.println("Product's constructor...");
	}
	private String halou(){
		return success;
	}
}

input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<form action="product-save.action" method="post">
		
		ProductName: <input type="text" name="productName"/>
		<br><br>

		ProductDesc: <input type="text" name="productDesc"/>
		<br><br>
		
		ProductPrice: <input type="text" name="productPrice" />
		<br><br>
		
		<input type="submit" value="Submit"/>
		<br><br>
	
	</form>

</body>
</html>

details.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	ProductId: ${productId}
	<br><br>
	ProductDesc: ${productDesc}
	<br><br>
	ProductPrice: ${productPrice }
	<br><br>
	ProductPrice: ^<s:property value="productPrice"/>
	<br><br>
	<%= request %>
	
</body>
</html>

对应关系如下

jsp中的<a href="product"> </a>和struts中的

<action =“product” class= “com.wj.aciton.login” method="save">

<result name="success">/WEB-INF/pages/login.jsp</result>

</action>

public class login(){

return success;

}

在action中访问web资源

1什么是web资源

HttpServietRequest,HttpSession ,ServletContext 等原生的Servlet API

2为什么访问WEB资源

B/S的应用的controller 中必然需要访问web资源 :像域对象中读写属性,读写cookie,获取realpath。。。。

3如何访问

1:和serviet API 解耦的方式

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login" method="post">
	id:<input type="text" name="id"><br>
name:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit"><br>
<%
 application.setAttribute("date",new Date());
%>

	</form>
</body>
</html>

struts.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: 包. struts2 使用 package 来组织模块. 
		name 属性: 必须. 用于其它的包应用当前包. 
		extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
		         struts-default 这个包在 struts-default.xml 文件中定义.
		namespace 可选, 如果它没有给出, 则以 / 为默认值. 
		                        若 namespace 有一个非默认值, 则要想调用这个包里的Action, 
		                        就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里
		                        
		          http://localhost:8080/contextPath/namespace/actionName.action
	-->
    <package name="helloWorld" extends="struts-default">
    	
    	<!-- 
    		配置一个 action: 一个 struts2 的请求就是一个 action 
    		name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
    		class 的默认值为: com.opensymphony.xwork2.ActionSupport
    		method 的默认值为: execute
    		result: 结果. 
    	-->
    	<!-- <action name="product-input" 
    		class="com.opensymphony.xwork2.ActionSupport"
    		method="execute"> -->
    		<!--  
    			result: 结果. 表示 action 方法执行后可能返回的一个结果. 所以一个 action 节点可能会有多个 result 子节点.
    			多个 result 子节点使用 name 来区分
    			name: 标识一个 result. 和 action 方法的返回值对应. 默认值为 success
    			type: 表示结果的类型. 默认值为 dispatcher(转发到结果.)
    		-->
    	<action name="login" class="com.wj.acion.TestLogin" method="login"    >
    	<result name="success">WEB-INF/pages/success.jsp</result>
    	</action>
    </package>

</struts>

TestLogin.java
package com.wj.acion;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class TestLogin {

	 public String  login(){
		
		//0:获取ActionContext对象
		 //ActionContext是action的上下文对象,可以获取当前action 的一切信息
		    ActionContext ac=ActionContext.getContext();  
		//1:获取application对应的map并向其中添加一个属性
		    Map<String,Object> map1=ac.getApplication();
		    //设置属性
		    map1.put("id", "11");
		    //获取属性
		    Object date1 = ac.get("date");
		    System.out.println("时间是:"+date1);
		    //2:session
		    Map<String,Object> session=ac.getSession();  
		    session.put("username", "zhan111gsan");//常用  
		    //3:request
		    //ActionContext并没有提供getRequest 方法来获取request
		    //需要手工调用require方法并传入"request"
		    Map<String,Object> req=(Map<String, Object>) ac.get("request");
		    req.put("password", "845376854");
		    //4获取请求参数,并获取指定的参数
		    Map<String,Object> parameters=ac.getParameters();
		    System.out.println(parameters.get("id"));
		 
		return "success";
	}
	
	
}

success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
        
        id: ${id} <br>
        name: ${username}<br>
        password: ${password}<br>
        today: ${date}<br>
        
</body>
</html>


2:和serviet API耦合的方式



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值