Struts2学习第一天--Struts2的概述、Struts2的入门、Struts2常见的配置、Struts2的Action的编写

 

 

 

action的name要与访问路径对应。hello.action。

加到tomcat启动 访问:http://localhost:8080/struts2-1/demo1/demo1.jsp

 

 

改为success

以上。

执行流程见下:

 代码:

package com.itheima.struts.demo1;
/**
 * Struts2的入门的Action类
 * @author jt
 */
public class HelloAction {

	/**
	 * 提供一个方法:
	 *  * 方法签名固定的
	 *  共有的 返回值是String类型 方法名execute 在这个方法中不能传递参数。
	 */
	public String execute(){
		System.out.println("HelloAction执行了...");
		return "success";
	}
}

 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>
	<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
	<!-- 配置Struts2的包 ================ -->
	<package name="demo1" extends="struts-default" namespace="/">
		<!-- 配置Action================ -->
		<action name="hello" class="com.itheima.struts.demo1.HelloAction" >
			<!-- 配置页面的跳转=========== -->
			<result name="success">/demo1/success.jsp</result>
		</action>
	</package>
	
</struts>

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>
<h1>Struts2的入门</h1>
<h3><a href="${ pageContext.request.contextPath }/hello.action">Struts2的入门</a></h3>
</body>
</html>

  

<%@ 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>
	<h1>跳转成功页面!!!</h1>
</body>
</html>

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>struts2_day01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置Struts2的核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  	<!-- 修改常量 -->
	<!--<init-param> -->
	<!--<param-name>struts.action.extension</param-name> -->
	<!--<param-value>xyz</param-value> -->
	<!--</init-param> -->
  </filter>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

  

当用户访问某一个Action的时候,先经过核心过滤器,在核心过滤器中执行一组拦截器(这组拦截器实现部分功能),执行目标Action,根据Action的返回值,进行页面跳转。

 

修改为abc后 再用.action访问就失效了 只能用XXX.abc进行访问。

properties需要在src目录下新建一个 但是只能用来修改常量。

web.xml里面是通过过滤器的初始化参数进行修改。

如果三个文件都修改了 那么web.xml里面的会最终生效。这与加载顺序有关 后加载的会覆盖以前的。

虽然如此 但我们习惯于struts.xml中修改。(一般一进入文件就可以看到。)

c此处代码不附上。

此处代码不附。

启动访问:http://localhost:8080/struts2-1/actionDemo3.action

以上三种方式 推荐使用第三种。

 

 

 

 

 

 

 代码如下:

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>
<h1>Action的访问</h1>
<h3>通过method方式</h3>
<a href="${ pageContext.request.contextPath }/userFind.action">查询用户</a><br/>
<a href="${ pageContext.request.contextPath }/userUpdate.action">修改用户</a><br/>
<a href="${ pageContext.request.contextPath }/userDelete.action">删除用户</a><br/>
<a href="${ pageContext.request.contextPath }/userSave.action">保存用户</a><br/>

<h3>通过通配符的方式</h3>
<a href="${ pageContext.request.contextPath }/product_find.action">查询商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_update.action">修改商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_delete.action">删除商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_save.action">保存商品</a><br/>

<h3>通过动态方法访问的方式</h3>
<a href="${ pageContext.request.contextPath }/customer!find.action">查询客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!update.action">修改客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!delete.action">删除客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!save.action">保存客户</a><br/>
</body>
</html>

  三个action:

package com.itheima.struts.demo3;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	public String find(){
		System.out.println("查询用户...");
		return NONE;
	}
	public String update(){
		System.out.println("修改用户...");
		return NONE;
	}
	public String delete(){
		System.out.println("删除用户...");
		return NONE;
	}
	public String save(){
		System.out.println("保存用户...");
		return NONE;
	}
}

  

package com.itheima.struts.demo3;

import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport {

	public String find(){
		System.out.println("查询商品...");
		return NONE;
	}
	public String update(){
		System.out.println("修改商品...");
		return NONE;
	}
	public String delete(){
		System.out.println("删除商品...");
		return NONE;
	}
	public String save(){
		System.out.println("保存商品...");
		return NONE;
	}
}

  

package com.itheima.struts.demo3;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {

	public String find(){
		System.out.println("查询客户...");
		return NONE;
	}
	public String delete(){
		System.out.println("删除客户...");
		return NONE;
	}
	public String update(){
		System.out.println("修改客户...");
		return NONE;
	}
	public String save(){
		System.out.println("保存客户...");
		return NONE;
	}
}

 

见下:

<?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>
	<!-- 开启动态方法访问 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	
	<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
	<!-- 配置Struts2的包 ================ -->
	<package name="demo3" extends="struts-default" namespace="/">
		<action name="userFind" class="com.itheima.struts.demo3.UserAction" method="find"></action>
		<action name="userUpdate" class="com.itheima.struts.demo3.UserAction" method="update"></action>
		<action name="userDelete" class="com.itheima.struts.demo3.UserAction" method="delete"></action>
		<action name="userSave" class="com.itheima.struts.demo3.UserAction" method="save"></action>
		
		
		<!-- 通配符的方式 -->
		<action name="product_*" class="com.itheima.struts.demo3.ProductAction" method="{1}"></action>
		
		<!-- 动态方法访问的方式 -->
		<action name="customer" class="com.itheima.struts.demo3.CustomerAction"></action>
		
	</package>
	
</struts>

 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>
	<!-- 配置Struts2的常量 -->
	<constant name="struts.action.extension" value="action"/>
	
	<include file="com/itheima/struts/demo1/struts_demo1.xml"></include>
	<include file="com/itheima/struts/demo2/struts_demo2.xml"></include>
	<include file="com/itheima/struts/demo3/struts_demo3.xml"></include>
</struts>

  

其中有俩jstl的包 standard 和jstl。

CREATE TABLE `cst_customer` (
  `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
  `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '固定电话',
  `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT '移动电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

 新建包结构:

在页面menu.html中修改。

 然后引入jstl的包 以便使用。

 同时修改jsp。

再在hibeinate映射文件中引入映射

数据库中添加两条数据:

再次启动项目:

http://localhost:8080/struts2_crm/

 

 代码略;

原文地址:https://www.cnblogs.com/ttty/p/10741755.html