Struts2,大爷你好!第二天

Struts2

第一节 Struts2 get/set 自动获取/设置数据

HelloWorldAction.java

package com.java1234.action;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action{
    private String name;  //自动获取数据get set方法
    
    public String getName() {
        return name;
    }

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

    @Override
    public String execute() throws Exception {
        System.out.println("执行了Action的默认方法");
        return SUCCESS;
    }

}

struts.xml

<?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="helloWorld" extends="struts-default">
      <action name="hello" class="com.java1234.action.HelloWorldAction2">
          <result name="success">helloWorld.jsp</result>
      </action>
      
 
  </package>

</struts>

helloWorld.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>
${name }大爷你好!
Struts2 get/set自动获取、设置数据
</body>
</html>



第二节ActionSupport类引入

HelloWorldAction2.java

package com.java1234.action;

import com.opensymphony.xwork2.ActionSupport;
//ActionSupport继承这个类,代替action接口
public class HelloWorldAction2 extends ActionSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name;  //自动获取数据get set方法
    
    public String getName() {
        return name;
    }

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

    @Override
    public String execute() throws Exception {
        System.out.println("执行了HelloWorldAction2的默认方法");
        return SUCCESS;
    }

}

struts.xml

<?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>  
      <action name="user" class="com.java1234.action.UserAction">
          <result name="success">success.jsp</result>
          <result name="error">error.jsp</result>
      </action>
 
  </package>

</struts>

index.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="post">
    name:<input type="text" name="name"/><input type="submit" value="Submit"/>
</form>
</body>
</html>



第三节Action设置数据
第一种方法:属性驱动(FieldDriven)A、基本数据类型属性 B、JavaBean类型属性

A、基本数据类型属性

UserAction.java

 

package com.java1234.action;

import com.java1234.model.User;
import com.java1234.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
//ActionSupport继承这个类,代替action接口
public class UserAction extends ActionSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //基本数据类型
    private String userName;
    private String password;
    
    private UserService userService=new UserService();

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("执行了UserAction的默认方法");
        User user=new User();
        user.setUserName(userName);
        user.setPassword(password);
        if(userService.login(user)){
            return SUCCESS;//login返回true,则登录成功页面
        }else{
            return ERROR; //失败返回错误页面
        }
    
    }

}

 

struts.xml

<?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="helloWorld" extends="struts-default">
      <action name="user2" class="com.java1234.action.UserAction2">
          <result name="success">success.jsp</result>
          <result name="error">error.jsp</result>
      </action>
 
  </package>

</struts>

User.java

package com.java1234.model;

public class User {
    private String userName;
    private String password;
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

 

 

UserService.java

package com.java1234.service;

import com.java1234.model.User;

public class UserService {
    public boolean login(User user){
        if("fdx".equals(user.getUserName())&&"123456".equals(user.getPassword())){
            return true;
        }else{
            return false;
        }
    }
}

 

 

login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="user" method="post">
    用户名:<input type="text" name="userName"/>
    密码:<input type="text" name="password"/>
    <input type="submit" value="登录"/>
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
登录成功!
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
登录失败!
</body>
</html>

 

  B、JavaBean类型属性

UserAction2.java

 

package com.java1234.action;

import com.java1234.model.User;
import com.java1234.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
//ActionSupport继承这个类,代替action接口
public class UserAction2 extends ActionSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //JavaBean类型属性
    private User user;
    private UserService userService=new UserService();
    
    public User getUser() {
        return user;
    }


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


    @Override
    public String execute() throws Exception {
        System.out.println("执行了UserAction2的默认方法");
        if(userService.login(user)){
            return SUCCESS;//login返回true,则登录成功页面
        }else{
            return ERROR; //失败返回错误页面
        }
    
    }

}

login2.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="user2" method="post">
    用户名:<input type="text" name="user.userName"/>
    密码:<input type="text" name="user.password"/>
    <input type="submit" value="登录"/>
</form>
</body>
</html>

struts.xml

<?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="helloWorld" extends="struts-default">
      <action name="hello" class="com.java1234.action.HelloWorldAction2">
          <result name="success">helloWorld.jsp</result>
      </action>
      
      <action name="user" class="com.java1234.action.UserAction">
          <result name="success">success.jsp</result>
          <result name="error">error.jsp</result>
      </action>
      
      <action name="user2" class="com.java1234.action.UserAction2">
          <result name="success">success.jsp</result>
          <result name="error">error.jsp</result>
      </action>
 
  </package>

</struts>

 


第二种方式:模型属性(ModelDriven)

这个方法不怎么用

 

posted @ 2019-03-25 10:41  小白咚  阅读(181)  评论(0编辑  收藏  举报