应付期末考的struts框架学习
使用前提 和简单代码
导入框架Jar文件

在web.xml插入语句
<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>
在源包下创建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>
<package name="default" namespace="/" extends="struts-default">
<!--name设置id 后面对应的类为action类-->
<action name="hello" class="action.HelloAction">
<result name="hello">/hello.jsp</result>
</action>
</package>
</struts>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package action;
/**
*
* @author 郑佳杰
*/
public class HelloAction {
public String execute(){
return "hello";
//对应<result name="hello">/hello.jsp</result>
//跳转到hello.jsp
}
}

简单案例 实现两数和
主界面
<%--
Document : input
Created on : 2021-3-31, 12:12:13
Author : 郑佳杰
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提交页面</title>
</head>
<body>
<form action="add" method="post">
请输入两个数:
<br>
加数:<input name="x"/>
<br>
被加数<input name="y"/>
<br>
<input type="submit" value="求和">
</form>
</body>
</html>
正数和负数的界面
<%--
Document : Positive
Created on : 2021-3-31, 12:16:49
Author : 郑佳杰
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>结果为正数</title>
</head>
<body>
结果为正数:<s:property value="sum"/>
</body>
</html>
<%--
Document : negative
Created on : 2021-3-31, 12:20:25
Author : 郑佳杰
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>结果为负数</title>
</head>
<body>
结果为负数:<s:property value="sum"/>
</body>
</html>
Action(最关键的地方)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package action;
/**
*
* @author 郑佳杰
*/
public class SumAction {
private int x;
private int y;
private int sum;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public int getSum() {
return sum;
}
public String execute(){
sum=x+y;
if(sum>=0) return "+";
else return "-";
}
}
配置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="default" namespace="/" extends="struts-default">
<action name="hello" class="action.HelloAction">
<result name="hello">/hello.jsp</result>
</action>
<!--add对应表单的action名字-->
<action name="add" class="action.SumAction">
<result name="+">Positive.jsp</result>
<result name="-">Negative.jsp</result>
</action>
</package>
</struts>
Action类的实现的3种方式

普通的Java类作为Action

继承ActionSupport实现Action

Action接口提供一个实现类ActionSupport

Action类获得参数的3种方式
属性驱动

对象驱动





浙公网安备 33010602011771号