Struts2——核心知识

Posted 程序员的时光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2——核心知识相关的知识,希望对你有一定的参考价值。

1,Struts2中get/set自动获取/设置数据

比如先在Action类里面添加一个name属性并加上get/set方法;

 
   
   
 
  1. package com.java.action;


  2. import com.opensymphony.xwork2.Action;


  3. public class HelloWorld implements Action {


  4. private String name;


  5. public String getName() {

  6. return name;

  7. }


  8. public void setName(String name) {

  9. this.name = name;

  10. }


  11. @Override

  12. public String execute() throws Exception {


  13. System.out.println("执行了action的默认方法");

  14. return SUCCESS;

  15. }

  16. }

然后我们在页面上再加上获取属性的代码;

 
   
   
 
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>

  2. <html>

  3. <head>

  4. <title>$Title$</title>

  5. </head>

  6. <body>

  7. ${name}&nbsp;Struts2

  8. </body>

  9. </html>

我们来测试一下看看后台能否获取到属性:

运行项目之后:

2,ActionSupport类引入

我们可以继承自ActionSupport类,修改如下:

 
   
   
 
  1. package com.java.action;


  2. import com.opensymphony.xwork2.ActionSupport;



  3. public class HelloWorld extends ActionSupport {



  4. private String name;


  5. public String getName() {

  6. return name;

  7. }


  8. public void setName(String name) {

  9. this.name = name;

  10. }


  11. @Override

  12. public String execute() throws Exception {


  13. System.out.println("执行了ActionSupport的默认方法");

  14. return SUCCESS;

  15. }

  16. }

运行出来是同样的结果;

3,Action设置数据

属性驱动

先建立一个user实体类;

 
   
   
 
  1. package com.java.model;


  2. public class User {


  3. private String userName;

  4. private String passWord;


  5. public String getUserName() {

  6. return userName;

  7. }


  8. public void setUserName(String userName) {

  9. this.userName = userName;

  10. }


  11. public String getPassWord() {

  12. return passWord;

  13. }


  14. public void setPassWord(String passWord) {

  15. this.passWord = passWord;

  16. }

  17. }

写一个简单的登录页面;

 
   
   
 
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>

  2. <html>

  3. <head>

  4. <title>Title</title>

  5. </head>

  6. <body>

  7. <form action="user" method="post">

  8. 用户名:<input type="text" name="userName"/>

  9. 密码:<input type="text" name="passWord"/>

  10. <input type="submit" value="登录"/>

  11. </form>

  12. </body>

  13. </html>

数据访问层service;

 
   
   
 
  1. package com.java.service;


  2. import com.java.model.User;


  3. public class UserService {


  4. public boolean login(User user){

  5. if("java".equals(user.getUserName()) && "123".equals(user.getPassWord())){

  6. return true;

  7. } else{

  8. return false;

  9. }

  10. }

  11. }

处理类Action;

 
   
   
 
  1. package com.java.action;


  2. import com.java.model.User;

  3. import com.java.service.UserService;

  4. import com.opensymphony.xwork2.ActionSupport;


  5. public class UserAction extends ActionSupport {


  6. private UserService userService=new UserService();


  7. private String userName;

  8. private String passWord;


  9. public String getUserName() {

  10. return userName;

  11. }


  12. public void setUserName(String userName) {

  13. this.userName = userName;

  14. }


  15. public String getPassWord() {

  16. return passWord;

  17. }


  18. public void setPassWord(String passWord) {

  19. this.passWord = passWord;

  20. }


  21. @Override

  22. public String execute() throws Exception {

  23. System.out.println("默认执行了User方法");

  24. User user=new User();

  25. user.setUserName(userName);

  26. user.setPassWord(passWord);

  27. if(userService.login(user)){

  28. return SUCCESS;

  29. } else{

  30. return ERROR;

  31. }

  32. }

  33. }

struts.xml配置;

 
   
   
 
  1. <action name="user" class="com.java.action.UserAction">

  2. <result name="success">success.jsp</result>

  3. <result name="error">error.jsp</result>

  4. </action>

运行之后就可以成功!

JavaBean属性

UserAction类;

 
   
   
 
  1. package com.java.action;


  2. import com.java.model.User;

  3. import com.java.service.UserService;

  4. import com.opensymphony.xwork2.ActionSupport;


  5. public class UserAction2 extends ActionSupport {


  6. private UserService userService=new UserService();


  7. private User user;


  8. public User getUser() {

  9. return user;

  10. }


  11. public void setUser(User user) {

  12. this.user = user;

  13. }


  14. @Override

  15. public String execute() throws Exception {

  16. System.out.println("默认执行了User方法");

  17. if(userService.login(user)){

  18. return SUCCESS;

  19. } else{

  20. return ERROR;

  21. }

  22. }

  23. }

登录界面修改一下;

 
   
   
 
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>

  2. <html>

  3. <head>

  4. <title>Title</title>

  5. </head>

  6. <body>

  7. <form action="user2" method="post">

  8. 用户名:<input type="text" name="user.userName"/>

  9. 密码:<input type="text" name="user.passWord"/>

  10. <input type="submit" value="登录"/>

  11. </form>

  12. </body>

  13. </html>

就可以了!


以上是关于Struts2——核心知识的主要内容,如果未能解决你的问题,请参考以下文章

Struts2 核心知识

初识Struts2

Struts2知识复习之一

Struts2知识点总结

Struts2知识复习之三

Struts2的知识点小总结