经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
IDEA基于Maven Struts2搭建配置及示例
来源:cnblogs  作者:52Hz  时间:2018/10/11 9:32:48  对本文有异议

1.web.xml加载struts框架即过滤器,要注意struts版本不同过滤器配置也不同。

  1. 1 <!DOCTYPE web-app PUBLIC
  2. 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. 3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. 4
  5. 5 <web-app>
  6. 6 <display-name>Archetype Created Web Application</display-name>
  7. 7
  8. 8 <filter>
  9. 9 <filter-name>struts2</filter-name>
  10. 10
  11. 11 <filter-class>
  12. 12 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  13. 13 </filter-class>
  14. 14
  15. 15 <init-param>
  16. 16 <param-name>config</param-name>
  17. 17 <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
  18. 18 </init-param>
  19. 19 </filter>
  20. 20
  21. 21 <filter-mapping>
  22. 22 <filter-name>struts2</filter-name>
  23. 23 <url-pattern>/*</url-pattern>
  24. 24 </filter-mapping>
  25. 25
  26. 26 <welcome-file-list>
  27. 27 <welcome-file>login.jsp</welcome-file>
  28. 28 </welcome-file-list>
  29. 29
  30. 30 </web-app>
View Code

2.配置struts.xml,配置Action。(name,class,method)

  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2
  3. 3 <!DOCTYPE struts PUBLIC
  4. 4 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  5. 5 "http://struts.apache.org/dtds/struts-2.5.dtd">
  6. 6
  7. 7 <struts>
  8. 8 <package name="default" extends="struts-default" namespace="/">
  9. 9 <action name="login" class="UserAction" method="login">
  10. 10 <result name="success">index.jsp</result>
  11. 11 <result name="login">login.jsp</result>
  12. 12 </action>
  13. 13 </package>
  14. 14 </struts>
View Code

3.View:login.jsp和index.jsp。

  1. 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. 2 pageEncoding="UTF-8"%>
  3. 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. 4 <html>
  5. 5 <head>
  6. 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. 7 <title>Hello Maven</title>
  8. 8 </head>
  9. 9 <body>
  10. 10 <p>欢迎进入Maven Struts2应用!</p>
  11. 11 </body>
  12. 12 </html>
View Code
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>登录界面</title>
  8. </head>
  9.  
  10. <body>
  11. <form action="login" method="post">
  12. <table>
  13. <tr>
  14. <td>用户名:</td>
  15. <td><input type="text" name="username" /> </td>
  16. </tr>
  17. <tr>
  18. <td>密码:</td>
  19. <td><input type="text" name="password" /> </td>
  20. </tr>
  21. <tr>
  22. <td colspan="2">
  23. <input type="submit" value="登录" />
  24. <input type="reset" value="重置" /></td>
  25. </tr>
  26. </table>
  27. </form>
  28. </body>
  29. </html>
View Code

4.编写Action处理类。

  1. 1 import com.opensymphony.xwork2.ActionSupport;
  2. 2 import org.apache.struts2.ServletActionContext;
  3. 3 import javax.servlet.http.HttpServletRequest;
  4. 4 import java.io.UnsupportedEncodingException;
  5. 5
  6. 6 public class UserAction extends ActionSupport{
  7. 7 @Override
  8. 8 public String execute() throws Exception {
  9. 9 return super.execute();
  10. 10 }
  11. 11 public String login() throws UnsupportedEncodingException {
  12. 12 HttpServletRequest request = ServletActionContext.getRequest();
  13. 13 request.setCharacterEncoding("utf-8");
  14. 14 String name = request.getParameter("username");
  15. 15 String pass = request.getParameter("password");
  16. 16 if("admin".equals(name)&&"".equals(pass)){
  17. 17 return SUCCESS;
  18. 18 }else{
  19. 19 return "login";
  20. 20 }
  21. 21 }
  22. 22 }
View Code

总结:初学struts期间遇到的struts.xml文件放置位置、struts过滤器配置、Maven依赖安装、struts流程原理等问题欢迎咨询!

转载请注明出处,谢谢!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号