Integrated SSH Framework (2)
SPRING4+HIBERNATE4+STRUTS2 integration, integration after the completion of the project I will upload it, but my advice is best or oneself in their own computer integration, I do not guarantee that there will be no problem
1, before the integration of STRUTS2
We have integrated Spring4 and hibernate4 in the front, and we continue to integrate STRUTS2
Introduce some Struts2 packages.
Some of them are repetitive, such as commons-logging this bag, and this everyone looks at the deletion
2, start the integration, we first configure the Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/javaeehttp://java.sun.com /xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "><!--instantiate a spring container in a Web container--><context-param>< param-name>contextconfiglocation</param-name><param-value>classpath:applicationcontext.xml</ param-value><!--This configuration file that specifies spring, by default looking for a configuration file from the Web root, we can use the classpath: prefix specified by spring to find--></from a classpath context-param><!--instantiation of spring container--><listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener><filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class><!--< Init-param> <param-name>config</param-name> <param-value>struts.xml</param-value> </init-param> </filter> <filt Er-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> & lt;/filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></ Welcome-file-list></web-app>
The Struts2 part is the same as the configuration of the individual Struts2, and the difference is that it joins the instantiation of spring with Web. xml
Let's take a look at the file with this Org.springframework.web.context.ContextLoaderListener to monitor spring instantiation. Where did you put the instantiation file???
We open this kind of source code, find this method, go into the initwebapplicationcontext inside to see,
In the source code we can find
Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, This.context);
Now that we know, he's put it in the ServletContext, and we'll take it out in the back.
3, Next we continue to configure the Struts2.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts><package name=" person "extends=" Struts-default "><action name=" Personaction "class=" cn.cutter_point.web.action.PersonAction "method=" execute "><result name=" list ">page/ Personlist.jsp</result></action></package></struts>
We'll take all the persons objects as represented by the person class we created earlier.
Let's write our action class to handle jumps and call the business
Personaction.java
/** * Features: Integrated SSH framework * author:cutter_point * Time: March 29, 2015 17:30:07 */package Cn.cutter_point.web.action;import Javax.servlet.servletcontext;import Javax.servlet.http.httpservletrequest;import Org.apache.struts2.servletactioncontext;import Org.springframework.web.context.webapplicationcontext;import Org.springframework.web.context.support.webapplicationcontextutils;import Cn.cutter_point.service.PersonService ; Import Com.opensymphony.xwork2.actionsupport;public class Personaction extends Actionsupport {@Overridepublic String Execute () throws Exception {//Get instance, method 1ServletContext sc = servletactioncontext.getrequest (). GetSession (). Getservletcontext (); Webapplicationcontext WAC = (webapplicationcontext) sc.getattribute (webapplicationcontext.root_web_application_ Context_attribute);//method 2WebApplicationContext Webapplicationcontext = Webapplicationcontextutils.getrequiredwebapplicationcontext (Servletactioncontext.getservletcontext ()); if (WAC = = Webapplicationcontext) {System.out.println ("OK!!!");} PersoNservice Personservice = (personservice) wac.getbean ("Personservicebean"); HttpServletRequest request = Servletactioncontext.getrequest (); Request.setattribute ("Persons", Personservice.getpersons ()); return "List";}}
OK, if you look at the above code carefully, you will find that there are two ways to instantiate spring, but in the end, I do not know, anyway, two of the things taken out are identical, no difference
Struts2 GET request Three ways
There are three ways to get the request in struts2, preferably using the Servletrequestaware interface to inject the request object through the IOC mechanism.
In action, get the request method one:
The code in the action:
Map request = (map) actioncontext.getcontext (). Get ("request");
list<task> tasks = Taskmanager.findall ();
Request.put ("Tasks", tasks);
Get the values in the JSP page:
<s:iterator id= "task" value= "#request. Tasks" >
<trclass= "Table_header" >
<td><s:propertyvalue= "#task. Tname"/></td>
<td><s:propertyvalue= "#task. Tuid"/></td>
<td><s:propertyvalue= "#task. Tstarttime"/></td>
<td><s:propertyvalue= "#task. Tendtime"/></td>
<td><s:propertyvalue= "#task. Tstate"/></td>
<td><inputtype= "Radio" id= "Choose" name= "Choose" onclick= "GetId (this.value)" value= "<s:propertyvalue=" #task. Tid '/> '/></td>
</tr>
</s:iterator>
--------------------------------------------------------------------------------------------
Method Two: Through the Servletactioncontext class to obtain, using the STRUTS2 experience if processing get parameter is Chinese, can only use this method to deal with garbled problem
Code in Action:
HttpServletRequest request =servletactioncontext.getrequest ();
Request.setattribute ("username", "Zhangsan");
Get the values in the JSP
<s:propertyvalue= "#request. Username" > or ${requestscope.req}
--------------------------------------------------------------------------------------------
Method Three: Inject the request object through the IOC mechanism via the Servletrequestaware interface
The code in the action:
Action implements the Servletrequestaware interface to implement the methods in the interface
private HttpServletRequest request;
Implementing methods in an interface
public void Setservletrequest (httpservletrequestrequest) {
This.request = Request;
}
In the Execute () method, you can then use the
Public String execute () {
Request.setattribute ("username", "Zhangsan");
Request.getsession (). Getservletcontext (). Getapplication (); Get application
}
The method must be implemented, and the method is automatically called
This method, in the process of being called, passes the created Request object to you through parameters, which you can assign to the variables in this class, and then request to use the
Note: the Setservletrequest () method must be executed before the Execute () method is called
4. Let's write our JSP file next
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib prefix= "s" uri= "/struts-tags" %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Here, let's expand the knowledge of Struts2 's label.STRUTS2 tags in use, when to use #, when can not be used #For example, I think some of this is written: <s:iterator id= "page" value= "List" ></s:iterator><s:iterator id= "page" value= "#List" > </s:iterator> which could say the details of the point, do not say in what root not root, this tangled up my long.
objects in the value stack do not use #, objects in non-value stacks use # If you don't understand the value stack, simply understand: the current action, or the property owned by the action in the action chain, and provides a getter and setter method for that property, Then you do not need to use # in the JSP, in addition to use the #, then you can know from here that the properties of the action if the getter and setter methods are provided, then these property values will be placed in the value stack
Struts tags <s:iterator> How to useThe list object in the action, in the page with <s:iterator> the contents of the list of the loop display first there is a user object public class user { Private Integer userid; private String username; Private String userpwd; //get and set Method .....} The code in the action list List = new ArrayList (); User User1 = new user (), User1.setuserid (1), User1.setusername ("Zhangsan"), User1.setuserpwd ("123456"); List.add (user1 ); User User2 = New user (), User2.setuserid (2), User2.setusername ("Lisi"), User2.setuserpwd ("1234"); List.add (User2); In the list in the request, in the JSP page can get the JSP page code <table><s:iterator value= "list" id= "Alias" >//this ID plus not all right <tr> <td><s:property value= "User.userid"/>< /td>//This can also be written as <s:property value= "alias. User.userid"/> <td> <s:property value= "User.username"/></td> <td><s:property value= "User.userpwd"/></td> </tr></s: Iterator></table>, that's it!
Reference:
http://zhidao.baidu.com/question/172130287
5. Start TomcatOpen Http://localhost:8080/ssh/PersonAction (I'm using Tomcat, specifically Tomcat, not here to wordy)
Console Run Results:
Project: http://download.csdn.net/detail/cutter_point/8544889
"Java EE Spring" 9, integrated SSH framework (2)