第一步,引入struts2-convention-plugin-2.2.1.jar

然后,改动配置文件。

我是在struts.properties文件里改动的:

struts.objectFactory = spring
struts.devMode = true
struts.i18n.encoding = UTF-8
struts.convention.result.path =/WEB-INF/jsp/
struts.convention.package.locators = action,actions,struts,struts2,control,controls,test
struts.convention.action.suffix =Action,Control

第一行。与spring整合。

第二行,启用struts开发模式。方便调试。

第三行,i18n

第四行,指定默认视图的路径,全部视图资源将从此路径下搜索。

第五行。指定搜索的包名。因为个人喜欢将控制类的包命名为*.control,所以增加control包。又增加了test包

第六行。指定class文件的文件结尾名。比方,默认仅仅搜索AbcAction这种类。如今也可搜索AbcControl这种类。


Java測试代码(部分)项目地址:http://localhost:8080/Photo/

package com.lgh.test;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.lgh.common.tools.json.JsonUtil;
import com.lgh.sys.entity.User;
import com.opensymphony.xwork2.Action;

@Scope("prototype")
@Controller
@Namespace("/name")//指定命名空间
public class TestControl implements Action {

	@Autowired
	private TestService testService;

	private User user;
	private String name;

	private List<User> List;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
        // http://localhost:8080/Photo/name/test 能够訪问到此action
	@org.apache.struts2.convention.annotation.Action(value = "test", results = {
			@Result(name = "success", location = "/index.jsp", type = "redirect"),
			@Result(name = "register", location = "/haha.jsp", type = "redirect") })
	public String test() throws Exception {
		List = testService.findBySome("1", User.class);
		user = testService.findBySome("1", User.class).get(0);
		name = user.getName();
		JsonUtil.outToJson(ServletActionContext.getResponse(), user);
		return SUCCESS;
	}

// http://localhost:8080/Photo/haha/register 能够訪问到此action 注意此处value以斜杠"/"开头 表示绝对路径了。namespace失效
	@org.apache.struts2.convention.annotation.Action(value = "/haha/register", results = {
			@Result(name = "success", location = "/index.jsp", type = "redirect"),
			@Result(name = "register", location = "./haha.jsp", type = "redirect") }) //运行后会跳转到 http://localhost:8080/Photo/haha/haha.jsp ./haha.jsp和直接写haha.jsp效果一样,可是写/haha.jsp之后,会跳转到 http://localhost:8080/Photo/haha.jsp 相似于绝对路径
	public String register() throws Exception {
		List = testService.findBySome("1", User.class);
		user = testService.findBySome("1", User.class).get(0);
		name = user.getName();
		// JsonUtil.outToJson(ServletActionContext.getResponse(), user);
		return "register";
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}

SSH整合后的框架下载,请用MyEclipse导入。

MySQL的driver和jackson1.9.11-all的jar包没有上传。请自行搜索下载。

http://download.csdn.net/detail/lgh06/8039749 免积分 全然免费哦。

写的比較烂,见谅……