Struts 2 기초 지식
11072 단어 struts2
JAVA web
Java Web ,Jsp , , ,
web :
HTML Java ,JSP , Java 。
:
,
:
(JSP,HTML )
(JavaBean)
(spring )
:
。
web.xml welcome
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
JSP pageEncoding contentType :
pageEncoding jsp
contentType charset
Struts2
common-logging-1.0.4.jar Struts2
freemarker-2.3.8.jar Struts2 UI
ognl-2.6.11.jar
strut2-core-2.0.11.jar Struts
xwork-2.0.4.jar xwork ,Struts2
Struts 2 , 。
STRUTS2 :
1. Tomcat
Window->References->Tomcat
2. JDK , JRE JAVA build path
Window->References->Java->Installed JREs
3.
,copy qulified name
4.Struts
1.web.xml struts filter
2.struts.xml src 。 ACTION
3.lib WEB-INF\lib
5. struts.xml , struts.xml , Tomcat
<constant name="struts.devMode" value="true" />
6. Eclipse lib ?
Referenced Libraries , lib , Java Source Attachement,
C:/Users/anker/Desktop/Java /Struts2/struts-2.1.6-all/struts-2.1.6/src/core/src/main/java
7. lib
Referenced Libraries , lib , Java Location
file:/C:/Users/anker/Desktop/Java /Struts2/struts-2.1.6-all/struts-2.1.6/docs/struts2-core/apidocs/
, F1
8. :
Copy , Tomcat , 。
:
, , Properties->MyEclipse->Web, Web Context-root
9.Struts2
<package name="default" namespace="/path" extends="struts-default">
<action name="path" class="com.bjsxt.struts2.front.action.StudentAction">
<result name="SUCCESS">
/indexAction.jsp
</result>
</action>
</package>
package action
Namespace: , namespace="" namespace="/", action , namespace 。
extends:
action: , .Action
class: , Struts ActionSupport , execute , success 。
result:
name: jsp , , "success" 。
global result : namespace , action
<global-results>
<result name="mainpage">login.jsp</result>
<global-results>
Action, namespace action , action。
<default-action-ref name="index"/>
<action name="index">
<result>/default.jsp</result>
</action>
<include file="login.xml"/>,login.xml struts.xml
10.Action
1. , 。
2. Action
3. ActionSupport , Struts2 。( )
Action , Strut2 。 , Action 。
Struts1 , 。 。
11.Action
1. , Action Execute 。
2. XX!method.action ,XX Action ,method Action
3. Action , method= "add" , Action , add 。
12.Action ?
1. URL 。 Action 。
http://localhost:8080/Struts2_0700_ActionAttrParamInput/path/path!GetMessage.action?name=Anker&age=18
path Action GetMessage , Anker,18 Action name age
2. ( )
, 。 Action get,set 。
http://localhost:8080/Struts2_0700_ActionAttrParamInput/path/path!GetMessage.action?user.name=Anker&user.age=18
: Action , , 。
3.ModelDriven
ModelDriven , getModel , 。
13. Struts.xml
, 。 , " "
ActionName * , class,method result , {1} * ,
{2} * ,
<package name="actions" extends="struts-default" namespace="/actions">
<action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
<result>/Student{1}_succes.jsp</result>
</action>
<action name="*_*" class="com.bjsxt.struts2.action.{1}Action">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
14. :
:
Windows->Preferences-> JSP
Encoding Chinese , National Standard, GB18030。 。
15.JSP
struts2 action jsp ,
MyEclipse jsp , :
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
path
basePath = http://localhost: / /
head , <base href="<%=basePath%>">,
<head>
<base href="<%=basePath%>">
</head>
16. 1.6 , , @override。 1.5
JDK 1.6 :
Installed JRE 1.6
Java Compiler Level 1.6
Tomcat 6.x JDK 1.6
17.MVC
MVC
M : Model
C : Action
V: View
C , Model View 。
18. Tomcat ?
%TOMCAT% /conf/server.xml :
<Connector port="9090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
19.Struts2 Web
(request,session,application)
request < session < application
request: ( , ) 。
session: , sessionID, cookie 。
session , .
application: application , ,application 。
request : javax.servlet.http.HttpServletRequest
session : javax.servlet.http.HttpSession
application : javax.servlet.ServletContext
setAttribute(String name, object o), getAttribute(String name)
Struts2 , request,session,application
1. ActionContext , MAP
2. RequestAware,SessionAware,ApplicationAware,Struts set 。 MAP<String,object>( )
3. ServletActionContext , HttpServletRequest,HttpSession,ServletContext。
4. ServletRequestAware ,Struts set 。 HttpServletRequest,HttpSession,ServletContext。
IOC DI
IOC : Inverse Of Control 。 ,
DI : Dependency Injection, , NEW
20. javaScript
<input type="button" value="submit1" onclick="javascript:document.f.action='login/login1.action';document.f.submit();">
<input type="button" value="submit2" onclick="javascript:document.f.action='login/login2.action';document.f.submit();">
<input type="button" value="submit3" onclick="javascript:document.f.action='login/login3.action';document.f.submit();">
<input type="button" value="submit4" onclick="javascript:document.f.action='login/login4.action';document.f.submit();">
21. addFieldError s:fieldError
,
this.addFieldError("name", "name is error");
Struts
<%@taglib uri="/struts-tags" prefix="s" %>
Action fielderror
<s:fielderror fieldName="name" theme="simple"/>
<s:debug> </s:debug>
Value Stack
: Property name Property Value
, , 。
<s:property value="errors.name[0]"/> value Stacke 。
errors property name,
name map key,[0] 。
22. JSP ?
jsp , open with, MyEclipse JSP Editor
23.ACTION , ?
<constant name="struts.i18n.encoding" value="GBK"/>
struts 2.16 bug, struts.xml , 。 2.17 。
24. result type
<result type="dispatcher" name="SUCCESS">/indexAction.jsp</result>
dispatcher( , 。 jsp forward, jsp )
redirect ( jsp, value stack 。 action Context )
chain( action, )
redirectAction( action)
25.
1.
forward , URL, URL , . ,
.
redirect , , . URL.
2.
forward: request .
redirect: .
3.
forward: , .
redirect: .
4.
forward: .
redirect: .。
26.OGNL
(value stack) action : <s:property value="username"/>
(get,set ): <s:property value="user.age"/>
<s:debug></s:debug>
Action ,Struts 。 。
27.struts2 exception
, throws , struts.xml
<exception-mapping result="error" exception="java.lang.Exception"/>
global , , package
<package name="bbs2009-default" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception" />
</global-exception-mappings>
</package>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
한 편의 문장으로 Struts2의 유형 전환을 해결하다우리는 HTTP를 통해 백그라운드에 제출된 데이터가 모두 문자열의 형식이라는 것을 알고 있으며, 우리가 필요로 하는 데이터 형식은 당연히 문자열 형식 하나만 있는 것이 아니다.그래서 우리는 유형 전환이 필요하다!다음...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.