一,struts的配置
????????导入jar包也就是maven的pom.xml
pom.xml
? ?
导入相关的依赖 会自动出现这些jar包
????????web.xml配置中央控制器也就是过滤器
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> ? <display-name>Archetype Created Web Application</display-name> ?? ?<filter> ?? ??? ?<filter-name>struts</filter-name> ?? ??? ?<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> ?? ?</filter>
?? ?<filter-mapping> ?? ??? ?<filter-name>struts</filter-name> ?? ??? ?<url-pattern>*.action</url-pattern> ?? ?</filter-mapping> </web-app> ?
?
? ? ? 写子控制器继承ActionSupport
<?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> ?? ?<!-- 引入struts的默认配置 --> ?? ?<include file="struts-default.xml"></include> ?? ?<!-- 对struts框架的基本配置 --> ?? ?<include file="struts-base.xml"></include> ?? ?<!-- 项目涉及到模块,分文件管理 --> ?? ?<include file="struts-sy.xml"></include> </struts>
http://locahost:8080/mvc/xxx.action?methodName=list;
http://locahost:8080/mvc/order/xxx.action
http://locahost:8080/mvc/book/xxx.action
二,struts的动态方法调用
子控制器
?配置
<?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>
{1}只的是调用第一个方法 ?? ?<package name="sy" extends="base" namespace="/sy"> ?? ??? ?<action name="/demo1_*" class="com.hmf.one.web.Demo1Action" method="{1}"> ?? ??? ??? ?<result name="bookEdit">/bookEdit.jsp</result> ?? ??? ?</action> ?? ?</package> </struts>
jsp
效果?
?
?
三,struts的传参
自定义mvc :
????????Modeldriver接口传参
struts:
????????①Modeldriver接口传参
public class Demo1Action extends ActionSupport implements ModelDriven<User>{ ?? ? private User user1=new User();
public String list() throws Exception { ?? ??? ?System.out.println("list方法"); ?? ??? ?System.out.println(user1); ?? ??? ?return "bookEdit"; ?? ?}? ?? ?public User getModel() { ?? ??? ?return user1; ?? ?}
}
实体类?
<h2>struts传参</h2> ?? ?<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">Modeldriver接口传参</a><br> ?
效果
?
?
? ? ? ? ②set传参
?? ?private String sex;
?? ?public String getSex() { ?? ??? ?return sex; ?? ?}
System.out.println(sex);
?? ?<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">set传参</a><br>
效果
?
? ? ? ? ③通过对象属性传参
?? ?private User user2;
?? ?<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user1.uid=002&&user2.uname=hmf">对象属性传参</a><br>
效果
四,struts与tomcat的集成
方法一:
?
效果
?方法二:
子控制器
package com.hmf.one.web;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware;
import com.hmf.one.entity.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven;
public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{ ?? ?private HttpServletResponse resp; ?? ?private HttpServletRequest req; ?? ? ?? ?private User user1=new User(); ?? ?private String sex; ?? ?private User user2; ?? ? ?? ? ?? ?public User getUser1() { ?? ??? ?return user1; ?? ?}
?? ?public void setUser1(User user1) { ?? ??? ?this.user1 = user1; ?? ?}
?? ?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() throws Exception { ?? ??? ?System.out.println("add方法"); ?? ??? ?return "bookEdit"; ?? ?} ?? ? ?? ?public String edit() throws Exception { ?? ??? ?System.out.println("edit方法"); ?? ??? ?return "bookEdit"; ?? ?} ?? ? ?? ?public String del() throws Exception { ?? ??? ?System.out.println("del方法"); ?? ??? ?return "bookEdit"; ?? ?} ?? ? ?? ?public String list() throws Exception { ?? ??? ?System.out.println("list方法"); ?? ??? ?System.out.println(user1); ?? ??? ?System.out.println(sex); ?? ??? ?System.out.println(user2); ?? ??? ?HttpServletRequest request = ServletActionContext.getRequest(); ?? ??? ?request.setAttribute("age", 18); ?? ??? ?req.setAttribute("hobby", "唱歌"); ?? ??? ?return "bookEdit"; ?? ?} ?? ? ?? ?public User getModel() { ?? ??? ?return user1; ?? ?}
?? ?public void setServletResponse(HttpServletResponse response) { ?? ??? ?this.resp=response; ?? ?}
?? ?public void setServletRequest(HttpServletRequest request) { ?? ??? ?this.req=request; ?? ?} } ?
?
效果
?
找个maven一定要有耐心等待!!!!!
|