struts/Hibernate/Spring(Glue) - > SSH
1:MVC/Service/Dao
2:Struts的版本
1:struts1 - >apache.org - > 第一个MVC框架。 高耦合,依赖于JSP/Servlet。99-07
2:自s1成功以后 ,就有了很多的其他的框架
Spring mvc
Xwork/webwork - >Struts2
Jsf - >SUN
网页:
http://struts.apache.org/download.cgi#struts25
下载以后:
3:快速入门
1:创建JavaWeb项目
2:导入struts2最少要求的包
3:在web.xml中配置struts2的核心过虑器
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4:添加struts2的配置文件,classpath: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>
</struts>
5:启动测试
如果没有包装则:
org.apache.catalina.connector.RequestFacade@7342b9
如果被Stuts2包装了则是:
org.apache.struts2.dispatcher.StrutsRequestWrapper@10fc6fa <br>
<%=request%>
6:接收用户的请求
可使用:
POJO - 简单Java对象
Action/ActionSupport
package cn.actions;
public class HelloAction {
//默认要求可以添加珍上方法,
public String execute(){
System.err.println("Hello this is:"+this);
return "ok";
}
}
7:将上面的类,配置到struts.xml
<!-- 声明你的action类,name将做为url的一部分可以访问
http://ip:port/project/hello
http://ip:port/project/hello.action
-->
<action name="hello" class="cn.actions.HelloAction">
<!--
retrun ok;,默认是转发到这个show.jsp
-->
<result name="ok">/jsps/show.jsp</result>
</action>
访问测试:
<a href="${pageContext.request.contextPath}/hello">Hello</a>
<br>
<a href="${pageContext.request.contextPath}/hello.action">Hello-2</a>
2:使用maven开发Struts2
1:创建maven项目
2:添加依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5</version>
</dependency>
3:在web.xml中配置struts2的核心过虑器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4:添加action
package cn.struts2.actions;
/**
* s2的action是多例的每一次访问都会创建一个新的实例
*
*/
public class OneAction {
public String execute(){
System.err.println("Hello 这是:"+this);
return "success";
}
}
5:配置action
<package name="oneone" extends="struts-default">
<action name="one" class="cn.struts2.actions.OneAction">
<!-- 如果没有给result设置名称,则默认的名称为name=success -->
<result>/jsps/show.jsp</result>
</action>
</package>
测试:
3:关于action
可以用三种方式开发action:
1:POJO 请参考 前面的。
2:实现接口action
必须要实现execute,可以使用常量 。
package cn.struts2.actions;
import com.opensymphony.xwork2.Action;
public class TwoAction implements Action {
@Override
public String execute() throws Exception {
System.err.println("Hello TwoAction is:"+this);
return SUCCESS;
}
}
<!-- 配置第二个action -->
<action name="two" class="cn.struts2.actions.TwoAction">
<result>/jsps/show.jsp</result>
</action>
3:继承ActionSupport
public String execute() throws Exception {
return SUCCESS;
}
package cn.struts2.actions;
import com.opensymphony.xwork2.ActionSupport;
public class ThreeAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.err.println("Three action is:"+this);
return SUCCESS;
}
}
<action name="three" class="cn.struts2.actions.ThreeAction">
<result>/jsps/show.jsp</result>
</action>
4:Struts2的配置文件
配置文件顺序 |
|
|
Web.xml |
|
|
default.properties | Struts2-core.jar |
|
Struts-default.xml | Struts2-core.jar |
|
Struts-plugin.xml | 导入的插件包中 |
|
struts.xml | 你息的配置文件 |
|
struts.properties | 你息的配置文件 |
|
如修改配置:
struts.action.extension=action,,
<!-- 配置常量 -->
<constant name="struts.action.extension" value="one,wj,two"></constant>
5:接收参数的两种方式:
1:属性驱动
声明get/Set方法
package cn.struts2.actions;
/**
* s2的action是多例的每一次访问都会创建一个新的实例
*
*/
public class OneAction {
private String nm;
private String pwd;
public void setName(String name) {
this.nm = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String execute() {
System.err.println("Hello 这是:" + this + ",name is:" + nm + ",pwd is:" + pwd);
return "success";
}
}
http://localhost/160606/one?name=Jack&pwd=1234
2:模型驱动
public class TwoAction implements Action, ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;// 返回的对象将接收参数
}
@Override
public String execute() throws Exception {
System.err.println("Hello TwoAction is:" + this + "user is:" + user);
return SUCCESS;
}
}
package cn.struts2.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.struts2.domain.User;
public class ThreeAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
private String msg;
@Override
public User getModel() {
return user;
}
public String getMsg() {
return msg;
}
@Override
public String execute() throws Exception {
System.err.println("Three action is:" + this);
if (user.getName().equals("Jack") && user.getPwd().equals("1234")) {
return SUCCESS;
} else {
msg = "你的用户名或是密码错误";
return ERROR;
}
}
}
<action name="three" class="cn.struts2.actions.ThreeAction">
<result>/jsps/show.jsp</result>
<result name="error">/index.jsp</result>
</action>