标签:blog http io os ar java 文件 sp 2014
第一步,引入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
第二行,启用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 免积分 完全免费哦。写的比较烂,见谅……
标签:blog http io os ar java 文件 sp 2014
原文地址:http://blog.csdn.net/recordme/article/details/40106709