struts2基础(1)
来源:互联网 发布:网络用语dm是什么意思 编辑:程序博客网 时间:2024/06/05 14:52
1、Struts2介绍
2、手动配置Struts2步骤
<1>新建项目
<2>导入所需jar包
<3>配置web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<4>新建Struts2的配置文件:struts.xml
<5>添加dtd
<6>测试
3、 Struts1与Struts2的区别
<1>配置不同:
Struts1采用的是<servlet>标签
Struts2采用的是<filter>标签
<2>拦截不同
Struts1拦截的是*.do
struts2拦截的是*.action
<3>
struts1的action需要继承action
struts2的action不需要继承任何类
<4>
struts1的action方法返回值是ActionForward
struts2的action方法返回值是String
<5>
struts1的action中的方法有四个参数,与web是紧耦合
Struts2的action中的方法没有参数,与web是松耦合
示列代码:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
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>
<package name="mypackage" extends="struts-default">
<action name="studentsava" class="com.action.Savaaction" method="sava">
<result name="success">/sava.jsp</result>
</action>
</package>
</struts>
index.jsp
<body>
<form action="studentsava.action" method="post">
studentName:<input type="text" name="studentName"/><br/>
studentAge:<input type="text" name="studentAge"/><br/>
studentSex:<input type="text" name="studentSex"/><br/>
<input type="submit" value="保存"/>
</form>
</body>
SavaAction.java
package com.action;
public class Savaaction {
private String studentName;
private int studentAge;
private String studentSex;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
this.studentSex = studentSex;
}
public Savaaction(String studentName, int studentAge, String studentSex) {
super();
this.studentName = studentName;
this.studentAge = studentAge;
this.studentSex = studentSex;
}
public Savaaction() {
super();
// TODO Auto-generated constructor stub
}
public String sava(){
System.out.println("姓名:"+studentName+" 年龄:"+studentAge+" 性别:"+studentSex);
return "success";
}
}
sava.jsp
<body>
<h1>学生信息</h1>
studentName:${studentName}<br/>
studentAge:${studentAge }<br/>
studentSex:${studentSex }
</body>
<body>
<h1>学生信息</h1>
studentName:${studentName}<br/>
studentAge:${studentAge }<br/>
studentSex:${studentSex }
</body>
- struts2基础(1)
- Struts2基础(1)
- struts2(基础1)
- Struts2.1笔记(三):Struts2基础
- struts2+hibernate基础入门(1)
- Java学习笔记-struts2基础(1)
- struts2 1-基础篇
- struts2基础篇(1)
- struts2 基础(3)
- struts2基础(续)
- Struts2基础(2)
- Struts2基础(3)
- Struts2基础(4)
- struts2(基础2)
- struts2(基础3)
- struts2(基础4)
- Struts2基础学习(1)-Action
- Struts2基础复习系列(1)
- C# 将 HTML 转成纯文本
- LightOJ - 1030 Discovering Gold 期望
- Search a 2D Matrix
- 编程学习之谈
- lua如何截取中英文混合字符串
- struts2基础(1)
- 控件字体大小随内容变化而变化
- 基于android 社区app短信分享 发送回调事件实现
- jstl的formatNumber标签的四舍五入问题
- android使用隐藏api的方法(使用被@hide的api)
- APP 适配 iOS8,位置、通知等特性
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- Runtime.getRuntime().exec中命令含有括号问题