Struts2自动装配(+入门)

Struts2  入门

实现 的具体步骤:
1.加载类库
2.配置web.xml
3.开发视图层页面
4.开发控制层Action
5.配置struts.xml文件
6.部署运行项目

1.
[java]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <parent>  
  4.         <artifactId>Y2_SSH</artifactId>  
  5.         <groupId>cn.happy</groupId>  
  6.         <version>1.0-SNAPSHOT</version>  
  7.     </parent>  
  8.     <modelVersion>4.0.0</modelVersion>  
  9.     <artifactId>Struts2Base</artifactId>  
  10.     <packaging>war</packaging>  
  11.     <name>Struts2Base Maven Webapp</name>  
  12.     <url>http://maven.apache.org</url>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>junit</groupId>  
  16.             <artifactId>junit</artifactId>  
  17.             <version>3.8.1</version>  
  18.             <scope>test</scope>  
  19.         </dependency>  
  20.         <dependency>  
  21.             <groupId>org.apache.struts</groupId>  
  22.             <artifactId>struts2-core</artifactId>  
  23.             <version>2.3.4.1</version>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>org.apache.struts.xwork</groupId>  
  27.             <artifactId>xwork-core</artifactId>  
  28.             <version>2.3.4.1</version>  
  29.         </dependency>  
  30.   
  31.         <!--EL表达式-->  
  32.         <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-spec -->  
  33.         <dependency>  
  34.             <groupId>org.apache.taglibs</groupId>  
  35.             <artifactId>taglibs-standard-spec</artifactId>  
  36.             <version>1.2.1</version>  
  37.         </dependency>  
  38.         <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->  
  39.         <dependency>  
  40.             <groupId>org.apache.taglibs</groupId>  
  41.             <artifactId>taglibs-standard-impl</artifactId>  
  42.             <version>1.2.1</version>  
  43.         </dependency>  
  44.   
  45.         <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->  
  46.         <dependency>  
  47.             <groupId>javax.servlet.jsp.jstl</groupId>  
  48.             <artifactId>jstl-api</artifactId>  
  49.             <version>1.2</version>  
  50.         </dependency>  
  51.         <dependency>  
  52.             <groupId>javaee</groupId>  
  53.             <artifactId>javaee-api</artifactId>  
  54.             <version>5</version>  
  55.         </dependency>  
  56.     </dependencies>  
  57.     <build>  
  58.         <resources>  
  59.             <resource>  
  60.                 <directory>src/main/java</directory>  
  61.                 <includes>  
  62.                     <include>**/*.*</include>  
  63.                 </includes>  
  64.             </resource>  
  65.         </resources>  
  66.     </build>  
  67. </project>  


2.
[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   
  4.   <filter>  
  5.     <filter-name>struts2</filter-name>  
  6.     <filter-class>  
  7.       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  8.     </filter-class>  
  9.   </filter>  
  10.   <filter-mapping>  
  11.     <filter-name>struts2</filter-name>  
  12.     <url-pattern>/*</url-pattern>  
  13.   </filter-mapping>  
  14.   
  15.   <context-param>  
  16.     <param-name>contextConfigLocation</param-name>  
  17.     <param-value>classpath:struts.xml</param-value>  
  18.   </context-param>  
  19.   <listener>  
  20.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  21.   </listener>  
  22.   <welcome-file-list>  
  23.     <welcome-file>helloworld.jsp</welcome-file>  
  24.   </welcome-file-list>  
  25. </web-app>  


3.
[java]  view plain  copy
  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  2. <%--  
  3.   Created by IntelliJ IDEA.  
  4.   User: linlin  
  5.   Date: 2017/10/22  
  6.   Time: 9:22  
  7.   To change this template use File | Settings | File Templates.  
  8. --%>  
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  10. <html>  
  11. <head>  
  12.     <title>登录</title>  
  13. </head>  
  14. <body>  
  15. <s:form action="UserAction" method="post">  
  16.     <s:textfield name="name" ></s:textfield>  
  17.     <s:textfield name="pwd" ></s:textfield>  
  18.     <s:submit value="提交"/>  
  19. </s:form>  
  20. </body>  
  21. </html>  
成功的页面:
[java]  view plain  copy
  1. <%--  
  2.   Created by IntelliJ IDEA.  
  3.   User: linlin  
  4.   Date: 2017/10/22  
  5.   Time: 9:22  
  6.   To change this template use File | Settings | File Templates.  
  7. --%>  
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  9. <html>  
  10. <head>  
  11.     <title>Success</title>  
  12. </head>  
  13. <body>  
  14. <h1>登录成功!!!</h1>  
  15. </body>  
  16. </html>  


4.控制层
[java]  view plain  copy
  1. package cn.happy.struts02;  
  2.   
  3. import com.opensymphony.xwork2.Action;  
  4.   
  5. /** 
  6.  * Created by linlin on 2017/10/22. 
  7.  */  
  8. public class UserAction implements Action {  
  9.     private String name;  
  10.     private String pwd;  
  11.     public String execute() throws Exception {  
  12.   
  13.         if(name.equals("admin")&&pwd.equals("1")){  
  14.             return SUCCESS;  
  15.         }  
  16.        return ERROR;  
  17.     }  
  18.   
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     public String getPwd() {  
  28.         return pwd;  
  29.     }  
  30.   
  31.     public void setPwd(String pwd) {  
  32.         this.pwd = pwd;  
  33.     }  
  34. }  

5.action
[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <package name="default" namespace="/" extends="struts-default">  
  7.   
  8.         <action name="he" class="cn.happy.struts01.HelloWorld">  
  9.             <result name="helloworld">/helloworld.jsp</result>  
  10.         </action>  
  11.   
  12.         <action name="UserAction" class="cn.happy.struts02.UserAction">  
  13.             <result name="success">/success.jsp</result>  
  14.             <result name="error">/index.jsp</result>  
  15.         </action>  
  16.     </package>  
  17. </struts>  




然后进行部署项目  tomcat  然后 启动  
在 浏览器 输入 login页面  进行一系列 表单提交 等  。。。


2.自动装配:


web.xml一样的

struts.xml  没变

类有2个:

[java]  view plain  copy
  1. package cn.happy.struts02;  
  2.   
  3. /** 
  4.  * Created by linlin on 2017/10/22. 
  5.  */  
  6. public class UserInfo {  
  7.     private String name;  
  8.     private String pwd;  
  9.     public String getName() {  
  10.         return name;  
  11.     }  
  12.   
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.   
  17.     public String getPwd() {  
  18.         return pwd;  
  19.     }  
  20.   
  21.     public void setPwd(String pwd) {  
  22.         this.pwd = pwd;  
  23.     }  
  24. }  


[java]  view plain  copy
  1. package cn.happy.struts02;  
  2.   
  3. import com.opensymphony.xwork2.Action;  
  4. import com.opensymphony.xwork2.ModelDriven;  
  5.   
  6. /** 
  7.  * Created by linlin on 2017/10/22. 
  8.  */  
  9. public class UserAction implements Action,ModelDriven<UserInfo> {  
  10.     private UserInfo user=new UserInfo();  
  11.   
  12.     public String execute() throws Exception {  
  13.   
  14.         if(user.getName().equals("admin")&&user.getPwd().equals("1")){  
  15.             return SUCCESS;  
  16.         }  
  17.        return INPUT;  
  18.     }  
  19.   
  20.     public UserInfo getUser() {  
  21.         return user;  
  22.     }  
  23.   
  24.     public void setUser(UserInfo user) {  
  25.         this.user = user;  
  26.     }  
  27.   
  28.     public UserInfo getModel() {  
  29.         return user;  
  30.     }  
  31. }  

页面

[java]  view plain  copy
  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  2. <%--  
  3.   Created by IntelliJ IDEA.  
  4.   User: linlin  
  5.   Date: 2017/10/22  
  6.   Time: 9:22  
  7.   To change this template use File | Settings | File Templates.  
  8. --%>  
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  10. <html>  
  11. <head>  
  12.     <title>登录</title>  
  13. </head>  
  14. <body>  
  15. <s:form action="UserAction" method="post">  
  16.     <s:textfield name="user.name" ></s:textfield>  
  17.     <s:textfield name="user.pwd" ></s:textfield>  
  18.     <s:submit value="提交"/>  
  19. </s:form>  
  20. </body>  
  21. </html>  


3.耦合 解耦方法:

[java]  view plain  copy
  1. package cn.happy.Struts03;  
  2.   
  3. import com.opensymphony.xwork2.Action;  
  4. import com.opensymphony.xwork2.ActionContext;  
  5. import com.opensymphony.xwork2.util.ValueStack;  
  6. import org.apache.struts2.ServletActionContext;  
  7. import org.apache.struts2.interceptor.ServletRequestAware;  
  8. import org.apache.struts2.util.ServletContextAware;  
  9.   
  10. import javax.servlet.ServletContext;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpSession;  
  13. import java.util.Map;  
  14.   
  15. /** 
  16.  * Created by linlin on 2017/10/22. 
  17.  */  
  18. public class LoginAction implements Action ,ServletRequestAware,ServletContextAware{  
  19.     private HttpServletRequest request;  
  20.     private ServletContext servletContext;  
  21.     public String execute() throws Exception {  
  22.         System.out.println("==============");  
  23.        //  HttpSession session=request.getSession();  
  24.   
  25.      //   HttpSession session = ServletActionContext.getRequest().getSession();  
  26.            // session.setAttribute("username","张三");  
  27.   
  28.        ActionContext context = ActionContext.getContext();  
  29.         Map<String, Object> map = context.getSession();  
  30.         map.put("uname","hhh99");  
  31. //  
  32.       /*  HttpServletRequest request = ServletActionContext.getRequest(); 
  33.         ValueStack vs= (ValueStack)request.getAttribute("struts.valueStack"); 
  34.         vs.push(map);*/  
  35.   
  36.         //放入值栈  
  37. /*        ValueStack valueStack = ActionContext.getContext().getValueStack(); 
  38.         valueStack.push(map);*/  
  39.   
  40.         //servlet  Context  
  41.         ServletContext servletContext = ServletActionContext.getServletContext();  
  42.        // session.setAttribute("unsame","admin");  
  43.         servletContext.setAttribute("userna","Servlet Context 00000000");  
  44.   
  45.             return SUCCESS;  
  46.   
  47.     }  
  48.   
  49.     public void setServletRequest(HttpServletRequest httpServletRequest) {  
  50.         this.request=httpServletRequest;  
  51.     }  
  52.   
  53.     public HttpServletRequest getRequest() {  
  54.         return request;  
  55.     }  
  56.   
  57.     public void setRequest(HttpServletRequest request) {  
  58.         this.request = request;  
  59.     }  
  60.   
  61.     public void setServletContext(ServletContext servletContext111) {  
  62.         this.servletContext = servletContext111;  
  63.     }  
  64. }  

页面:
[java]  view plain  copy
  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  2. <%--  
  3.   Created by IntelliJ IDEA.  
  4.   User: linlin  
  5.   Date: 2017/10/22  
  6.   Time: 9:22  
  7.   To change this template use File | Settings | File Templates.  
  8. --%>  
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  10. <html>  
  11. <head>  
  12.     <title>Success</title>  
  13. </head>  
  14. <body>  
  15. <h1>登录</h1>  
  16. <h1><s:property value="#session.username"></s:property> </h1>  
  17. <h1><s:property value="#session.uname"></s:property> </h1>  
  18. <h1><s:property value="uname"></s:property> </h1>  
  19. <h1><s:property value="#session.userna"></s:property> </h1>  
  20. </body>  
  21. </html> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值