struts2 小案例

pom.xml

<dependencies>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>3.8.1version>
        <scope>testscope>
    dependency>
    
    <dependency>
        <groupId>javaeegroupId>
        <artifactId>javaee-apiartifactId>
        <version>5version>
    dependency>

    <dependency>
        <groupId>org.apache.strutsgroupId>
        <artifactId>struts2-coreartifactId>
        <version>2.3.4.1version>
    dependency>

    <dependency>
        <groupId>org.apache.struts.xworkgroupId>
        <artifactId>xwork-coreartifactId>
        <version>2.3.4.1version>
    dependency>

dependencies>

web.xml
 web-app PUBLIC
                "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>

<filter>
  <filter-name>struts2filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
  <filter-name>struts2filter-name>
  <url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
  <welcome-file>login.jspwelcome-file>
welcome-file-list>
web-app>

struts.xml
xml version="1.0" encoding="UTF-8" ?>
 struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="userAction" class="cn.happy.action.UserAction">
            <result name="success">/success.jspresult>
            <result name="input">/login.jspresult>
        action>
    package>
struts>
UserAction:
package cn.happy.action;

import cn.happy.entity.Userinfo;
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 Happy on 2017-10-22.
 *       类 Action
 */
public class UserAction implements Action,ServletRequestAware,ServletContextAware {
    //SpringMvc Controller
    private HttpServletRequest servletRequest;
    private ServletContext servletContext;
    private Userinfo userinfo;
    public String execute() throws Exception {
        if ("admin".equals(userinfo.getUsername())&&"admin".equals(userinfo.getPassword())){
            //登录成功获取的用户名
          /*    //第一种方式
            ActionContext context1=ActionContext.getContext();
            Map map = context1.getSession();
            map.put("username",userinfo.getUsername());*/
            //放入植栈
            /*ValueStack valueStack = ActionContext.getContext().getValueStack();
            valueStack.push(map);*/
          /*  HttpServletRequest request = ServletActionContext.getRequest();
            ValueStack vs= (ValueStack)request.getAttribute("struts.valueStack");
            vs.push(map);*/
            //第二种方式
            HttpSession session= ServletActionContext.getRequest().getSession();
            session.setAttribute("username",userinfo.getUsername());
           //第三种方式
           /* HttpServletRequest request=ServletActionContext.getRequest();
            request.getSession().setAttribute("username",userinfo.getUsername());*/
            //第四种方式
           /* ServletContext context=ServletActionContext.getServletContext();
            context.setAttribute("username",userinfo.getUsername());*/
            return SUCCESS;
        }else {
            return INPUT;
        }

    }

    public Userinfo getUserinfo() {
        return userinfo;
    }

    public void setUserinfo(Userinfo userinfo) {
        this.userinfo = userinfo;
    }

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

    public void setServletContext(ServletContext servletContext) {
     this.servletContext=servletContext;
    }
}
登录成功后的页面:
login.jsp:
 
  
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
<s:form method="POST" action="userAction">
    <%--性能低--%>
    请输入用户名: <s:textfield  name="userinfo.username">s:textfield> <br/>
    <s:textfield name="userinfo.password">s:textfield><br/>
    <s:submit value="登录">s:submit>
s:form>
body>
html>
success.jsp:
 
  
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Titletitle>
head>
<body>

<s:debug/>
<%--第一种方式
     欢迎--%>
<%--第二种方式--%>
   <%--欢迎--%>
<s:property value="#session.username">s:property>


body>
html>


action 继承 ActionSupport
public class fristAction extends ActionSupport {
    public  String del(){
        Map result=(Map)ActionContext.getContext().get("request");
        result.put("username","张三");
        return "del";
    }
   public String list(){
      Map result=(Map)ActionContext.getContext().get("request");
       Object username=result.get("username");
       System.out.println(username);
      result.put("username","张三");
        return "list";
}
}
配置:
 
  
xml version="1.0" encoding="UTF-8" ?>
 struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true"/>
    
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>

    
        

       
       
     
   
    <package name="cn.happy.day03manymethod" namespace="/" extends="struts-default">
        <action name="fristAction" class="cn.happy.day03manymethod.fristAction" method="list"  >
            
           
          
            <result name="{2}" type="redirectAction">
                <param name="actionName">addhhparam>
                <param name="namespace">/param>
            result>
        action>
        <action name="addhh" class="cn.happy.day03manymethod.fristAction" method="add"  >
            <result name="fristAction">/day-03/add.jspresult>
        action>
    package>
struts>
页面同上

你可能感兴趣的:(struts2 小案例)