艾特商业网

Struts 2迷你书(struts 2)

更新时间:2023-07-27 17:17:42

导读 你们好,最近小艾特发现有诸多的小伙伴们对于Struts 2迷你书,struts 2这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看吧

你们好,最近小艾特发现有诸多的小伙伴们对于Struts 2迷你书,struts 2这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看吧。

1、 从网上下载struts2需要的jar包。需要以下jar包。

2、 1.在myeclipse中创建新的web项目,随便起个名字。比如我的就叫logintest。

3、 右键单击logintest项目上的-- build path -- configure bulid path,单击libraries,单击add external jars.并选择struts2需要添加的jar包的位置。

4、 3.在WebRoot下找到用于配置的web.xml文件,并粘贴以下代码。

5、 ?xml version='1.0' encoding='UTF-8'?

6、 web-app version='2.5'

7、 xmlns='http://java.sun.com/xml/ns/javaee'

8、 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

9、 xsi:schemaLocation='http://java.sun.com/xml/ns/javaee

10、 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'

11、 ! -Define the core filter of Pillar 2, that is, the filter of the filter dispatcher of Pillar 2-

12、 filter

13、 filter-namestruts2/filter-name! -Define the name of the core filter-

14、 过滤级!-定义核心过滤器的实现类-

15、 org.apache.struts2.dispatcher.FilterDispatcher

16、 /filter-class

17、 /filter

18、 !-Core filter拦截所有请求,即使用FilterDispatcher初始化struts2并处理所有web请求-

19、 filter-mapping

20、 filter-namestruts2/filter-name

21、 url-pattern/*/url-pattern

22、 /filter-mapping

23、 /web-app

24、 在src中创建一个名为struts.xml的新xml文件,然后将以下代码粘贴到该文件中。

25、 ?xml version='1.0' encoding='UTF-8'?

26、 !DOCTYPE struts PUBLIC

27、 '-//Apache Software Foundation//DTD Struts Configuration 2.0//EN'

28、 'http://struts.apache.org/dtds/struts-2.0.dtd'

29、 struts

30、 !-如果package添加了namespace属性,jsp页面表单的action属性必须添加项目名称的路径-

31、 package name='struts2' extends='struts-default' namespace='/demo'

32、 action name='login' class='com.action.LoginAction'

33、 result/loginsuccess.jsp/result

34、 /action

35、 /package

36、 /struts

37、 在src中新建一个包,名为com.action,在这个包下新建一个名为LoginAction的类,并提升以下代码站。

38、 package com.action;

39、 import com.opensymphony.xwork2.ActionSupport;

40、 public class LoginAction extends ActionSupport{

41、 private String username;

42、 private String password;

43、 public String getUsername() {

44、 return username;

45、 }

46、 public void setUsername(String username) {

47、 this.username=username;

48、 }

49、 public String getPassword() {

50、 return password;

51、 }

52、 public void setPassword(String password) {

53、 this.password=password;

54、 }

55、 public String execute(){

56、 System.out.println('走进动作');

57、 return SUCCESS;

58、 }

59、 public String login(){

60、 return SUCCESS;

61、 }

62、 }

63、 在WebRoot下创建一个名为login.jsp的新jsp,并复制以下代码。

64、 %@ page language='java' import='java.util.*' pageEncoding='UTF-8'%

65、 %

66、 String path=request.getContextPath();

67、 String basePath=request.getScheme()+'://'+request.getServerName()+':'+request.getServerPort()+path+'/';

68、 %

69、 !DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'

70、 html

71、 head

72、 base href='%=basePath%'

73、

74、 titleMy JSP 'login.jsp' starting page/title

75、

76、 meta http-equiv='pragma' content='no-cache'

77、 meta http-equiv='cache-control' content='no-cache'

78、 meta http-equiv='expires' content='0'

79、 meta http-equiv='keywords' content='keyword1,keyword2,keyword3'

80、 meta http-equiv='description' content='This is my page'

81、 !--

82、 link rel='stylesheet' type='text/css' href='styles.css'

83、 --

84、 /head

85、 body

86、 form action='/attendance_book/demo/login.action' method='post'

87、 账号:input type=' text ' name=' username '/br/

88、 密码:input type=' text ' name=' password '/br/

89、 Input Type=' Submit' Value=' OK'

90、 /form

91、 /body

92、 /html

93、 在WebRoot下创建一个名为loginsuccess.jsp的新jsp,并复制以下代码。

94、 %@ page language='java' import='java.util.*' pageEncoding='UTF-8'%

95、 %

96、 String path=request.getContextPath();

97、 String basePath=request.getScheme()+'://'+request.getServerName()+':'+request.getServerPort()+path+'/';

98、 %

99、 !DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'

100、 html

101、 head

102、 base href='%=basePath%'

103、

104、 titleMy JSP 'loginsuccess.jsp' starting page/title

105、

106、 meta http-equiv='pragma' content='no-cache'

107、 meta http-equiv='cache-control' content='no-cache'

108、 meta http-equiv='expires' content='0'

109、 meta http-equiv='keywords' content='keyword1,keyword2,keyword3'

110、 meta http-equiv='description' content='This is my page'

111、 !--

112、 link rel='stylesheet' type='text/css' href='styles.css'

113、 --

114、 /head

115、 body

116、 用户名:${request.username}

117、 密码:${requestScope.password}

118、 /body

119、 /html

120、 将此项目添加到tomcat中,启动tomcat,在地址栏输入地址:http://127.0.0.1:8080/logintest/login.jsp,

121、 在此文本框中输入用户名和密码,会跳到下面的页面

以上就是struts 2这篇文章的一些介绍,希望对大家有所帮助。

免责声明:本文由用户上传,如有侵权请联系删除!