Struts2入门 自动装配 Servlet API

本文档介绍了Struts2框架的自动装配功能,通过web.xml配置与struts.xml的设置,展示了如何在类中实现自动装配。同时,讲解了Servlet API的基本使用,帮助初学者理解这两个关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts2  入门

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

1.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>Y2_SSH</artifactId>
        <groupId>cn.happy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>Struts2Base</artifactId>
    <packaging>war</packaging>
    <name>Struts2Base Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!--EL表达式-->
        <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-spec -->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javaee</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>


2.
<?xml version="1.0" encoding="UTF-8"?>
<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">

  <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>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:struts.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>helloworld.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3.
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/10/22
  Time: 9:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<s:form action="UserAction" method="post">
    <s:textfield name="name" ></s:textfield>
    <s:textfield name="pwd" ></s:textfield>
    <s:submit value="提交"/>
</s:form>
</body>
</html>
成功的页面:
<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/10/22
  Time: 9:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Success</title>
</head>
<body>
<h1>登录成功!!!</h1>
</body>
</html>


4.控制层
package cn.happy.struts02;

import com.opensymphony.xwork2.Action;

/**
 * Created by linlin on 2017/10/22.
 */
public class UserAction implements Action {
    private String name;
    private String pwd;
    public String execute() throws Exception {

        if(name.equals("admin")&&pwd.equals("1")){
            return SUCCESS;
        }
       return ERROR;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

5.action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">

        <action name="he" class="cn.happy.struts01.HelloWorld">
            <result name="helloworld">/helloworld.jsp</result>
        </action>

        <action name="UserAction" class="cn.happy.struts02.UserAction">
            <result name="success">/success.jsp</result>
            <result name="error">/index.jsp</result>
        </action>
    </package>
</struts>




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


2.自动装配:


web.xml一样的

struts.xml  没变

类有2个:

package cn.happy.struts02;

/**
 * Created by linlin on 2017/10/22.
 */
public class UserInfo {
    private String name;
    private String pwd;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}


package cn.happy.struts02;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

/**
 * Created by linlin on 2017/10/22.
 */
public class UserAction implements Action,ModelDriven<UserInfo> {
    private UserInfo user=new UserInfo();

    public String execute() throws Exception {

        if(user.getName().equals("admin")&&user.getPwd().equals("1")){
            return SUCCESS;
        }
       return INPUT;
    }

    public UserInfo getUser() {
        return user;
    }

    public void setUser(UserInfo user) {
        this.user = user;
    }

    public UserInfo getModel() {
        return user;
    }
}

页面
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/10/22
  Time: 9:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<s:form action="UserAction" method="post">
    <s:textfield name="user.name" ></s:textfield>
    <s:textfield name="user.pwd" ></s:textfield>
    <s:submit value="提交"/>
</s:form>
</body>
</html>


3.耦合 解耦方法:

package cn.happy.Struts03;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by linlin on 2017/10/22.
 */
public class LoginAction implements Action ,ServletRequestAware,ServletContextAware{
    private HttpServletRequest request;
    private ServletContext servletContext;
    public String execute() throws Exception {
        System.out.println("==============");
       //  HttpSession session=request.getSession();

     //   HttpSession session = ServletActionContext.getRequest().getSession();
           // session.setAttribute("username","张三");

       ActionContext context = ActionContext.getContext();
        Map<String, Object> map = context.getSession();
        map.put("uname","hhh99");
//
      /*  HttpServletRequest request = ServletActionContext.getRequest();
        ValueStack vs= (ValueStack)request.getAttribute("struts.valueStack");
        vs.push(map);*/

        //放入值栈
/*        ValueStack valueStack = ActionContext.getContext().getValueStack();
        valueStack.push(map);*/

        //servlet  Context
        ServletContext servletContext = ServletActionContext.getServletContext();
       // session.setAttribute("unsame","admin");
        servletContext.setAttribute("userna","Servlet Context 00000000");

            return SUCCESS;

    }

    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request=httpServletRequest;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletContext(ServletContext servletContext111) {
        this.servletContext = servletContext111;
    }
}

页面:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/10/22
  Time: 9:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Success</title>
</head>
<body>
<h1>登录</h1>
<h1><s:property value="#session.username"></s:property> </h1>
<h1><s:property value="#session.uname"></s:property> </h1>
<h1><s:property value="uname"></s:property> </h1>
<h1><s:property value="#session.userna"></s:property> </h1>
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值