Struts2第一步:
1.新建一个web项目,在web.xml中加入一个filter
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.导入struts2的jar包,可去官网下载
3.从例子中拷贝一个struts.xml的配置文件过来
下面是我做个第一个小例子:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="userlist" class="com.aowin.action.UserAction" method="getUsers">
<result>/list.jsp</result>
</action>
<action name="input" class="com.aowin.action.UserAction" method="addInput">
<result>/input.jsp</result>
</action>
<action name="update" class="com.aowin.action.UserAction" method="update">
<result>/update.jsp</result>
</action>
<action name="updateSubmit" class="com.aowin.action.UserAction" method="updateSubmit">
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="fail">/error.jsp</result>
<result name="updatefail">/updatefail.jsp</result>
<result name="updateSuccess">/updateSuccess.jsp</result>
<result name="invalid.token" type="redirectAction">
<param name="namespace">/user</param>
<param name="actionName">userlist</param>
</result>
</action>
<action name="addUser" class="com.aowin.action.UserAction" method="addUser">
<!-- strtus2 默认的拦截器stack 没有加上token这个拦截器 -->
<interceptor-ref name="token"></interceptor-ref>
<!-- 一旦配了单独的拦截器 默认的拦截器栈 就失效了 需要手动加上 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="invalid.token" type="redirectAction">
<param name="namespace">/user</param>
<param name="actionName">userlist</param>
</result>
<result name="fail">/error.jsp</result>
<result>/list.jsp</result>
</action>
<action name="deleteUser" class="com.aowin.action.UserAction" method="deleteUser">
<result type="redirectAction">
<param name="namespace">/user</param>
<param name="actionName">userlist</param>
</result>
</action>
</package>
</struts>
简单分为4个包 dao action model service 分别做增删改查的操作
action的代码:用util模拟了dao层 因为没用到数据库所以service也给放弃了
只有util模拟数据库 model 和 action
package com.aowin.action;
import java.util.List;
import java.util.Map;
import com.aowin.model.User;
import com.aowin.util.Util;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private int idss ;
private int id;
private User user;
private List<User> userList;
private Map request ;
public int getIdss() {
return idss;
}
public void setIdss(int idss) {
this.idss = idss;
}
public Map getRequest() {
return request;
}
public void setRequest(Map request) {
this.request = request;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
//1.开始在页面上显示 所有的 数据
public String getUsers() {
userList = Util.list;// 模拟去数据库操作
request = (Map) ActionContext.getContext().get("request");
request.put("userList", userList);
return Action.SUCCESS;
}
//仅仅是跳转到add界面
public String addInput() {
return Action.SUCCESS;
}
//点击修改后的进入到这个方法
public String update() {
//看看问号传进来的id是否有得到
request = (Map) ActionContext.getContext().get("request");
for (User u : Util.list) {
if (u.getId() == this.id) {
user = u;
}
}
request.put("user", user);
return Action.SUCCESS;
}
//修改后提交的方法updateSubmit()
public String updateSubmit() {
User us = null ;
//用一个us接住上传来的user 根据上传来的id得到原来的us us去setname 和address
userList = Util.list;
int id = user.getId() ;
String name = user.getName();
String address = user.getAddress() ;
if("".equals(name.trim()) || "".equals(address.trim())){
return "fail" ;
}
//看看是否需要setid
// user.setId(user.getId());
for (User u : userList) {
if(u.getName().equals(name.trim()) && u.getAddress().equals(address.trim())){
return "updatefail" ;
}
if (u.getId() == id) {
us = u;
}
}
us.setId(id);
us.setName(name);
us.setAddress(address);
request = (Map) ActionContext.getContext().get("request");
request.put("userList", userList);
return "updateSuccess" ;
}
//增加用户
public String addUser(){
userList = Util.list;
String name = user.getName();
String address = user.getAddress() ;
for(User u : userList){
System.out.println(u.getName());
if(u.getName().equals(name.trim()) || u.getAddress().equals(address.trim())||"".equals(name.trim())||"".equals(address.trim())){
return "fail" ;
}
}try{
id = userList.get(userList.size()-1).getId() + 1 ;
}catch (Exception e) {
id = 1 ;
}
user.setId(id);
user.setName(name);
user.setAddress(address);
userList.add(user);
request = (Map) ActionContext.getContext().get("request");
request.put("userList", userList);
return Action.SUCCESS;
}
//删除用户
public String deleteUser(){
userList = Util.list;
userList.remove(idss);
request = (Map) ActionContext.getContext().get("request");
request.put("userList", userList);
return Action.SUCCESS;
}
}
分享到:
相关推荐
标题 "http://topkinghat.iteye.com/blog/840706" 提到的博客链接实际上指向了“姜铁”的个人博客文章,而描述中的 "NULL" 暂无具体信息。不过,标签“源码”和“工具”暗示了这篇博客可能涉及到软件开发的源代码...
http://www.iteye.com/topic/699515 主题:Swing是一把刀 http://www.iteye.com/topic/702804 主题:Swing第二刀:枝间新绿一重重 http://www.iteye.com/topic/707540 主题:Swing第二小刀刀:星星之火可以燎原 ...
博文链接:https://jackzhangyunjie.iteye.com/blog/202349
从零开始学Spring Boot,没有积分的...(2)Spring Boot返回json数据【从零开始学Spring Boot】 http://412887952-qq-com.iteye.com/blog/2291508 (15)Spring Boot使用Druid和监控配置【从零开始学Spring Boot】 ...
NULL 博文链接:https://zw7534313.iteye.com/blog/426799
NULL 博文链接:https://adrain-work-163-com.iteye.com/blog/1545091
NULL 博文链接:https://timewalker.iteye.com/blog/1065615
NULL 博文链接:https://wincheer.iteye.com/blog/774298
读者如果曾经学习过Struts1.x或者有过Struts1.x的开发经验,那么千万不要想当然地...虽然Struts2的开发小组极力保留Struts1.x的习惯,但因为Struts2的核心设计完全改变,从思想到设计到工作流程,都有了很大的不同。
NULL 博文链接:https://qiusenvs.iteye.com/blog/412763
NULL 博文链接:https://wy649898543.iteye.com/blog/1423655
NULL 博文链接:https://yizhilong28.iteye.com/blog/1161027
NULL 博文链接:https://ownraul.iteye.com/blog/1277047
NULL 博文链接:https://hanazawakana.iteye.com/blog/1593221
NULL 博文链接:https://zzz299.iteye.com/blog/691049
博文链接:https://flare.iteye.com/blog/151376
博文链接:https://knight.iteye.com/blog/75888
博文链接:https://antgreen.iteye.com/blog/245704
博文链接:https://hkme.iteye.com/blog/142511
NULL 博文链接:https://breezedancer.iteye.com/blog/545781