其实struts2的零配置很好理解 先要配置web.xml 如下 (其中一段) <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.shop.struts2.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern> 拦截.action 方法
</filter-mapping>具体只写一个Action 就拿登录Action来说吧package com.shop.struts2.action;import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.shop.vo.User;
@Namespace(value = "/struts") //这里声明命名空间
public class LoginAction extends BaseAction {
private User user;
public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}
@Action(value = "login_Login", results = { // 这里相当于struts.xml <action name = xxx>
@Result(name = "login",location="/struts/login.jsp"), //这里是action 里面result 的name 和要跳转的页面,当然还可以指定其类型 type = "xxxx"
@Result(name = "center",location="/struts/center.jsp")
})
public String login() {
if(userService.login(user.getAccount(), user.getPassword()))
{
user = userService.find("account", user.getAccount());
System.out.println(user.getUserName());
ServletActionContext.getRequest().getSession(true).setAttribute("user", user);
ServletActionContext.getRequest().getSession(true).setAttribute("status", "success");
return "center";
}
return "login";
}
@Action(value = "logout_Login", results = {
@Result(name = "login",location="/struts/login.jsp"),
})
public String logout() {
ServletActionContext.getRequest().getSession(true).removeAttribute("user");
ServletActionContext.getRequest().getSession(true).removeAttribute("shopCart");
ServletActionContext.getRequest().getSession(true).setAttribute("status", "logout");
return "login";
}}不知道这样写好不好,如果有比这里更好的谢谢一起交流 我QQ4218380 或者加入 技术交流群 28566860 (Android技术交流)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货