【 Struts2 配置】Struts2基本搭建
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="struts2-hibernate" extends="struts-default" namespace="/">
<action name="student_*" class="k.action.StudentAction" method="{1}">
<result name="list" type="redirectAction">student_list</result>
<result name="toList">/WEB-INF/student/list.jsp</result>
<result name="toAdd">/WEB-INF/student/toAdd.jsp</result>
<result name="toUpdate">/WEB-INF/student/toUpdate.jsp</result> </action>
</package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <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> <listener>
<listener-class>k.util.StartSystemListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
StudentAction
package k.action; import com.opensymphony.xwork2.ActionSupport;
import k.domain.Student;
import k.service.StudentService;
import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest;
import java.util.List; public class StudentAction extends ActionSupport {
public String list() {
StudentService service = new StudentService();
List list = service.list();
HttpServletRequest request = ServletActionContext.getRequest();
if (list == null || list.size() <= 0) {
list.add(new Student("默认", 1));
}
request.setAttribute("list", list);
return "toList";
} public String toAdd() {
return "toAdd";
} public String add() {
HttpServletRequest request = ServletActionContext.getRequest();
String name = request.getParameter("name");
String age = request.getParameter("age");
StudentService service = new StudentService();
service.add(new Student(name, Integer.parseInt(age)));
return "list";
} public String toUpdate() {
HttpServletRequest request = ServletActionContext.getRequest();
String id = request.getParameter("id");
StudentService service = new StudentService();
Student model = service.getModel(id);
request.setAttribute("stu", model);
return "toUpdate";
} public String update() {
HttpServletRequest request = ServletActionContext.getRequest();
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
Student student = new Student(name, Integer.parseInt(age));
student.setId(Integer.parseInt(id));
StudentService service = new StudentService();
service.update(student);
return "list";
} public String delete() {
HttpServletRequest request = ServletActionContext.getRequest();
String id = request.getParameter("id");
StudentService service = new StudentService();
Student model = service.getModel(id);
service.delete(model);
return "list";
}
}
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>list</title>
</head>
<body>
<h1><a href="${APP_PATH}/student_toAdd.action">student_toAdd</a></h1>
<form>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>update</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td><a href="${APP_PATH}/student_toUpdate.action?id=${stu.id}">update</a></td>
<td><a href="${APP_PATH}/student_delete.action?id=${stu.id}">delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</body>
</html>
toAdd.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
</head>
<body>
<form action="${APP_PATH}/student_add.action" method="post">
<h2><input type="text" name="name" value="测试1"></h2>
<h2><input type="text" name="age" value="11"></h2>
<h2><input type="submit" value="submit"></h2>
</form>
</body>
</html>
toUpdate.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>update</title>
</head>
<body>
<form action="${APP_PATH}/student_update.action" method="post">
<h2><input type="text" name="id" value="${stu.id}"></h2>
<h2><input type="text" name="name" value="${stu.name}"></h2>
<h2><input type="text" name="age" value="${stu.age}"></h2>
<h2><input type="submit" value="submit"></h2>
</form>
</body>
</html>
【 Struts2 配置】Struts2基本搭建的更多相关文章
- web09 struts2配置 struts2入门
电影网站:www.aikan66.com 项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.co ...
- STRUTS2配置动态页面
STRUTS2配置动态页面 CreateTime--2017年5月11日09:00:31Author:Marydon 1.struts配置 <?xml version="1.0&q ...
- struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)
为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...
- struts2学习笔记--动手搭建环境+第一个helloworld项目
在Myeclipse中已经内置好了struts2的环境,但是为了更好的理解,这里自己从头搭建一下: 前期准备:下载struts2的完整包,下载地址:https://struts.apache.org/ ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
- Struts2 框架的快速搭建
方便myEclipse 手动配置Struts2框架,写下此文,需要的朋友拿走不谢~ 一.引入JAR包 WEB工程->WebRoot->WEB-INF->lib引入Struts2对应版 ...
- Struts2配置dtd约束
Struts2和Struts1的区别: 一.elclipse-ee开发 搭建环境eclipse-ee 1.加入jar包 apps/struts2-blank.war解压 2.在web.xml文件中配 ...
- 在Eclipse里面配置Struts2
下面介绍在Eclipse里面配置Struts2 下载Struts2的压缩包 我下载的是2.3.32版本 解压之后如图所示 apps目录:Struts2的范例 docs目录:Struts2的文档 lib ...
- Struts2框架学习(一)——Struts2的概念及搭建
一.Struts2的概念 使用优势:1)自动封装参数 2)参数校验 3)结果的处理(转发|重定向) 4)国际化 5)显示等待页面 6)防止表单重复提交 Struts2具有更加先进的架构以及思想 Str ...
- 配置struts2+spring,springmvc
Struts2+Spring整合 一.spring负责注入,struts2负责它自己的工作.这样不是很符合spring作为ioc容器的全部功能,不推荐. 二.spring负责全部bean和struts ...
随机推荐
- idea软件操作
1.快捷键: 1.1.格式化代码:crtl+alt+L 1.2.一些构造啊,setter/getter等方法:alt+insert 1.3.crtl + f 搜素当前页面
- [SDOI2017] 新生舞会 - 二分图最大权匹配,分数规划,二分答案
有一个二分图,每个部都有 \(n\) 个点,每条边有两个参数 \(a_e, b_e\),求一种匹配,使得 \(\sum a_i / \sum b_i\) 最大 Solution 显然的分数规划,考虑二 ...
- RN开发-Android原生交互
在使用RN开发过程中,难免有些原生功能需要要自己来实现,下面总结一下在使用RN与原生开发交互. 1.在原生代码中定义实现类 1.1 首先继承 ReactContextBaseJaveModule抽象 ...
- C语言移除链表元素
删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4 ...
- PyQt5+Eric6开发的一个使用菜单栏、工具栏和状态栏的示例
前言 在做一个数据分析的桌面端程序遇到一些问题,这里简单整理下,分享出来供使用者参考. 1.网上查使用PyQt5工具栏的示例,发现很多只是一个简单的退出功能,如果有几个按钮如何处理?如何区分点击的究竟 ...
- XSS挑战之旅,学习笔记
第一关: http://test.ctf8.com/level1.php?name=test 观察到通过get方式传参有会显, 直接打最简单的xss playload: <script>a ...
- 06 部署redis缓存数据库
1 安装redis $ sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,检查Redis服务器程序 注:在安装过程中,腾讯服务器会中途停止. ...
- 算法竞赛入门经典第二版 竖式问题 P42
#include<bits/stdc++.h> using namespace std; int inset(char *s,int num) { //判断数字是否在数字集中 int le ...
- 题解【AcWing177】噩梦
题面 考虑双向广搜. 我们需要记录男孩和女孩的当前位置,并且每次都进行扩展. 记录一个数组 \(st[i][j]\) . 如果 \(st[i][j]=0\) ,说明 \((i,j)\) 还没有被男孩和 ...
- Python之路Day07
基础数据类型补充 str s.capitalize() -- 首字母大写 s.title() -- 每个单词首字母大写 s.swapcase() -- 大小写转换 s.center() -- 居中/填 ...