struts2-convention-plugin插件

convention插件最重要的一条原则,约定优于配置,所以首先先熟悉一些约定规则,否则打死你也不知道为什么总是no mapping

一、按照约定部署Action类

        1、根据struts2文档,首先会寻找包名为action、actions、struts、struts2并会被映射为根包,而这些包下的子包被映射为对应的命名空间

        2、convention插件会从这些包或者子包中寻找实现接口com.opensymphony.xwork2.Action或者以Action结尾命名的类

       例如:

       com.example.actions.MainAction -> /
       com.example.actions.products.Display -> /products   实现了com.opensymphony.xwork2.Action接口
       com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details
       com.accp.struts2.action.HelloWorldAction    /action

       最后一个同时有strust2和aciton,插件会认为第一个struts2是根包,action是命名空间

       3、映射Action的name属性也需要遵循一定的约定规则

               如果该Action类名以Action结尾后缀,则该Action的name属性为,将该类名去掉后缀Action,首字母小写,如果没有以Action后缀,则直接首字母小写,将Action类名的

       驼峰写法转成中划线写法,所有字母小写,单词与单词之间用横线分割。

       例如:

      com.example.actions.MainAction -> /main
      com.example.actions.products.Display -> /products/display
      com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details
      com.accp.struts2.action.HelloWorldAction    /action/hello-world

      4、映射Result的约定规则

      默认情况下,Convention总会到Web应用下的/WEB-INF/content路径下定位物理资源,定位资源的约定可以参考下图

    result类型基于文件的扩展名,支持的文件扩展名为:jsp,ftl,vm,html。

   我自己写的一个简单的例子,之前总写不对,再写一次,记住它

   首先,将插件struts2-convention-plugin-XXX.jar添加至Web应用下的/WEB-INF/lib下

   Action类


package com.accp.struts2.action;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

	private static final long serialVersionUID = -5211484390830086897L;

	private String greeting;

	public String getGreeting() {
		return greeting;
	}

	public void setGreeting(String greeting) {
		this.greeting = greeting;
	}
	
	@Override
	public String execute() throws Exception {
		greeting = "hello every one!";
		return SUCCESS;
	}
	
}

请求的页面

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<base href="<%=basePath%>" />

		<title>零配置——Conversion插件的使用</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>
			零配置——Conversion插件的使用
		</h3>
		<s:a action="hello-world" namespace="/action">hello-world</s:a>
	</body>
</html>
结果页面,要配置在/WEB-INF/content下

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<base href="<%=basePath%>" />

		<title>Struts与Spring整合</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>
			Hello World!<br/>
			
		</h3>
			<s:property value="greeting"/>
			<br/>
			hello-world-success.jsp
	</body> 
</html>

Convention插件还有其他用法,先记住这个简单的,其他慢慢研究吧


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值