开发环境
- 开发工具:Eclipse
- 数据库:MySQL
- server:Tomcat
Struts2 请求原理流程图
构建一个 web maven project,在pom.xml引入struts2的jar包
<!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1.2</version>
</dependency>
在 web.xml 文件里引入struts2的过滤器
<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>
測试流程
- 创建一个TestAction类
package com.gx.action;
public class TestAction {
public String helloworld(){
return "hello";
}
}
2.在resources下创建struts.xml和test.xml文件
- test.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>
<package name="userPkg" namespace="/testspace" extends="struts-default">
<!-- http://localhost:8080/manningSampleApp/packageNamespace/actionName_method -->
<action name="testAction_*" class="com.gx.action.TestAction" method="{1}">
<result name="hello">/WEB-INF/test.jsp</result>
</action>
<!--
<action ...
-->
</package>
</struts>
- 在struts.xml文件里引入user.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>
<include file="test.xml"></include>
</struts>
3.浏览器输入:http://localhost:8080/struts2curd/testspace/testAction_helloworld
4. 结果:
写控制层代码
- BaseAction
package com.gx.base;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*
* BaseAction类
* 继承ActionSupport和实现了ModelDriven接口
* @author always
*
* @param <T>
*/
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
protected T model=null;
protected Class<T> modelClass;
/*存放json数据,用来实现ajax*/
protected Map<String, Object> jsonMap=new HashMap<String, Object>();
public BaseAction(){
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
modelClass=(Class) pt.getActualTypeArguments()[0];
}
public T getModel() {
if(model==null){
try {
model=modelClass.newInstance();
} catch (final Exception e) {
LOG.error(e.getMessage());
}
}
return model;
}
public Map<String, Object> getDataMap() {
return jsonMap;
}
public void setJsonMap(Map<String, Object> jsonMap{
this.jsonMap = jsonMap;
}
}
- SimpleMethodAction
package com.gx.base;
/**
* 基本curd方法的空实现,方便统一管理
*
* @author always
*
* @param <T>
*/
public abstract class SimpleMethodAction<T> extends BaseAction<T>{
/**
* 列表
* @return
* list.jsp
*/
public String list(){
return "list";
}
/**
* 删除
* @return
* toList-->actionName_list-->list.jsp(user.xml中配置)
*/
public String delete(){
return "toList";
}
/**
* 改动
* @return
* toList-->actionName_list-->list.jsp
*/
public String edit(){
return "toList";
}
/**
* 改动页面
* @return
* saveUI.jsp
*/
public String editUI(){
return "saveUI";
}
/**
* 加入
* @return
* toList-->actionName_list-->list.jsp
*
*/
public String add(){
return "toList";
}
/**
* 加入页面
* @return
* saveUI.jsp
*/
public String addUI(){
return "saveUI";
}
}
- user.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>
<package name="userPkg" namespace="/user" extends="json-default">
<!-- http://localhost:8080/manningSampleApp/packageNamespace/actionName_method -->
<!-- 普通用户管理 -->
<action name="userAction_*" class="com.gx.web.struts2.UserAction" method="{1}">
<!--
sturts框架自己主动转
要在pom.xml引入
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.2.3</version>
</dependency>
-->
<result type="json">
<param name="root">jsonMap</param>
</result>
<result name="list">/WEB-INF/jsp/user/list.jsp
</result>
<result name="saveUI">/WEB-INF/jsp/user/saveUI.jsp
</result>
<result name="toList" type="redirectAction">userAction_list</result>
</action>
</package>
</struts>
- UserAction
package com.gx.web.struts2;
import org.apache.log4j.Logger;
import com.gx.base.SimpleMethodAction;
import com.gx.domain.PageBean;
import com.gx.domain.User;
import com.gx.service.UserService;
import com.gx.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionContext;
/**
* 继承SimpleMethodAction
* @author always
*
*/
public class UserAction extends SimpleMethodAction<User> {
/**
*
*/
private static final long serialVersionUID = -8771772792039694213L;
static Logger log = Logger.getLogger(UserAction.class);
private UserService userService = new UserServiceImpl();
/*删除,改动 用户时传来的id*/
private int userId;
/*分页*/
private int pageNum = 1;
@Override
public String list() {
ActionContext ac = ActionContext.getContext();
PageBean pageBean = userService.getPageBean(pageNum, 5);
ac.getValueStack().push(pageBean);
return super.list();
}
public String listJson() {
jsonMap.clear();
ActionContext ac = ActionContext.getContext();
PageBean pageBean = userService.getPageBean(pageNum, 5);
jsonMap.put("pageBean", pageBean);
jsonMap.put("success", true);
return SUCCESS;
}
@Override
public String delete() {
userService.delete(userId);
return super.delete();
}
@Override
public String edit() {
User model = this.getModel();
userService.update(model);
return super.edit();
}
@Override
public String editUI() {
log.info(userId);
User user = userService.getById(userId);
log.info(user.toString());
ActionContext.getContext().getValueStack().push(user);
return super.editUI();
}
@Override
public String add() {
User model = this.getModel();
log.info(model.toString());
userService.save(model);
return super.add();
}
@Override
public String addUI() {
return super.addUI();
}
@Override
public void validate() {
log.info("開始校验");
super.validate();
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
}
jsp 代码
- list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>username列表</title>
<%@ include file="/WEB-INF/jsp/public/common.jspf" %>
</head>
<body>
<s:debug></s:debug>
<div class="container">
<!-- 分页信息 -->
<s:form id="pageform" action="userAction_list">
<table class="table">
<!-- 表头-->
<thead>
<tr>
<td>username</td>
<td>密码</td>
<td>邮箱</td>
<td>联系方式</td>
<td>相关操作</td>
</tr>
</thead>
<!--显示数据列表-->
<tbody>
<s:iterator value="recordList">
<tr>
<td>${username}</td>
<td>${password}</td>
<td>${email}</td>
<td>${telphone}</td>
<td>
<s:a action="userAction_delete?userId=%{id}" onclick="return confirm(‘您确定要删除么?‘)">删除</s:a>
<s:a action="userAction_editUI?
userId=%{id}">改动</s:a>
</td>
</tr>
</s:iterator>
<%@ include file="/WEB-INF/jsp/public/pageView.jspf" %>
</tbody>
</table>
</s:form>
<!-- 其它功能超链接 -->
<div>
<div>
<s:a action="userAction_addUI">加入</s:a>
</div>
</div>
</div>
</body>
</html>
- saveUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>username列表</title>
<%@ include file="/WEB-INF/jsp/public/common.jspf" %>
</head>
<body>
<s:debug></s:debug>
<div class="container">
<!-- 分页信息 -->
<s:form id="pageform" action="userAction_list">
<table class="table">
<!-- 表头-->
<thead>
<tr>
<td>username</td>
<td>密码</td>
<td>邮箱</td>
<td>联系方式</td>
<td>相关操作</td>
</tr>
</thead>
<!--显示数据列表-->
<tbody>
<s:iterator value="recordList">
<tr>
<td>${username}</td>
<td>${password}</td>
<td>${email}</td>
<td>${telphone}</td>
<td>
<s:a action="userAction_delete?userId=%{id}" onclick="return confirm(‘您确定要删除么?‘)">删除</s:a>
<s:a action="userAction_editUI?userId=%{id}">改动</s:a>
</td>
</tr>
</s:iterator>
<%@ include file="/WEB-INF/jsp/public/pageView.jspf" %>
</tbody>
</table>
</s:form>
<!-- 其它功能超链接 -->
<div>
<div>
<s:a action="userAction_addUI">加入</s:a>
</div>
</div>
</div>
</body>
</html>
为了方便,在这里我没有将js与jsp代码分离,最好的做法是jsp外部引用js文件,并将引用的文件放到html代码后</body>
前
版权声明:本文博客原创文章,博客,未经同意,不得转载。
时间: 2024-10-10 18:14:10