struts的基本介绍
Struts是Apache基金组织中Jakarta项目组的一个开源(Open Source)项目,主要就是实现了MVC设计模式,在Struts中有自己的控制器(ActionServlet),同时也提供了各种常用的页面标签库以减少JSP页面中的Scriptlet代码,Struts实际上就属于在传统技术上发展起来的一种新的应用模式,其操作的本质依然还是JSP、Servlet、JavaBean等技术的应用。
接下来我们来看一下sturts的配置文件
sturts-base.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="base" extends="struts-default" abstract="true">
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
</struts>
struts-sy.xml
系统的文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
</package>
</struts>
struts.xml
全局配置,有多少配置文件都得引用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<include file="struts-sy.xml"></include>
</struts>
1、动态动态方法调用
不需要指定父类(ActionSupport)
跟mvc不同的地方就是不需要往前台传methodName就可以直接可以调用!!
struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="/hello_*" class="com.chenkang.web.HelloAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
这里的 * 代表的是方法名!
像这样我们只要输入方法名就能调用到!
2.jsp的三种参数传递
1.set传参
2.属性参数名.属性名传参
3.实现modeldriven接口传参
HelloAction
package com.chenkang.web;
import com.chenkang.entity.User;
import com.opensymphony.xwork2.ModelDriven;
/**
* 1.动态调用方法(mvc不具备的优势)
* 2.struts中的传参
* 1.set传参
* 2.属性参数名.属性名传参
* 3.实现modeldriven接口传参
* 3.struts与Tomcat的交互
* @author Administrator
*
*/
public class HelloAction implements ModelDriven<User>{
private User user1 = new User();
private User user2;
private String sex;
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String add() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
return "success";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
}
demo1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>struts传参的三种方式</h2>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nv">测试set传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid==002&&user2.uname=lisi">测试属性名.属性名传参传参</a>
</body>
</html>
运行结果:
struts与J2EE容器交互
如何将后台的值传到前台去
1.request传值
2.值栈传值(get方法传值)
他有两种方式一种是非注入一种是注入
他们都有耦合和解耦的方式。。
非注入的耦合方式太过单一我们一般采用的是注入的耦合方式
如果说在一个类所有的方法都要用到就得用注入的方式!!
至于解耦的方式我们都不用因为里面是用的map集合需要去记类名!
*/
public class HelloAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletResponse response;
private HttpServletRequest request;
我们需要实现ServletRequestAware,ServletResponseAware接口并且重写他们的方法并且把值附上就可以了!
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
}