Shortly after getting started with struts of Jsp, I had to learn spring and then combine the two. So I found a lot of examples about struts + spring on the Internet, but they are basically EXAMPLES OF a, and then re-compile a book based on its principles to implement the example with struts.
I. Runtime Environment
1. eclipse-SDK-3.2
2. MyEclipse GA5.0
3. apache-tomcat-5.5.20
4. Strust1.2
5. Spring1.2
Ii. documents required for the project
1. index. jsp
2. login. jsp
3. reinput_blank.jsp
4. reinput_error.jsp
5. success. jsp
6. LoginActio. java
7. LoginForm. java
8. struts-config.xml
9. applicationContext. xml
Step 3
1. Build a project
Create a Web Project named Welcome_SS
File-> New-> Project-> Web Project, "next" Project Name: "Welcome_SS", "finish ";
2. Import the struts package
Right-click the Welcome_SS project and choose "Myeclipse" from the shortcut menu. Choose "Add struts Capabilities..." from the shortcut menu ......, Select struts1.2 and finish;
3. Import the spring package
Select the Welcome_SS project, right-click-> Myeclipse (or choose Myeclipse on the menu bar)-> Add spring Capabilities ......, By default, select Spring 1.2 AOP Libraries-<MyEclipse-Library> and Spring 1.2 Web Libraries-<MyEclipse-Library>, and then finish;
(Why do I have to select these two)
Note: you may need to add several spring packages under the lib folder,
Spring. jar,
Spring-1.2.6.jar,
Spring-aop.jar,
Spring-beans.jar,
Spring-context.jar,
Spring-core.jar,
Spring-orm.jar,
Spring-remoting.jar,
Spring-support.jar,
Spring-web.jar,
Spring-webmvc.jar
4. write code (pages and required classes)
Ø index. jsp
Select WebRoot-> New-> JSP under the Welcome_SS project, name it index. jsp, and Finish;
Enter the following code
1
2 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
3 4 5 <! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
6 7 <body> <div align = "center">
8 Welcome here! <Br>. <a href = "login. jsp"> sign in </a> </div>
9 </body>
10 11
Ø login. jsp
Select WebRoot-> New-> JSP under the Welcome_SS project, name it login. jsp, and Finish;
Enter the following code
1 <% @ page contentType = "text/html; charset = gb2312" %>
2 <% @ page import = "java. util. *, java. SQL. *, java. text. *, java. io. *" %>
3
4 5 <body>
6 <div align = "center"> <form name = "form1" method = "post" action = "login. do">
7 Welcome Here! <Br>
8 username: <input type = "text" name = "username"> <br>
9 password: <input type = "text" name = "password"> <br>
10 <input type = "submit" name = "submit1" value = "submit">
11 <input type = "reset" name = "submit2" value = "reset" onclick = "deleteAllMessage ()"> <br>
12 </form> </div>
13 </body>
14 Ø reinput_blank.jsp
Select WebRoot-> New-> JSP under the Welcome_SS project, and name it reinput_blank.jsp and Finish;
Enter the following code
1 <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
2
3 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
4 5 <body>
6 <div align = "center"> <form name = "form1" method = "post" action = "login. do "> <font color =" # ff0000 "> * the user name and password cannot be blank! </Font> <br>
7 username: <input type = "text" name = "username"> <br>
8 password: <input type = "text" name = "password"> <br>
9 <input type = "submit" name = "submit1" value = "submit">
10 <input type = "reset" name = "submit2" value = "reset" onclick = "deleteAllMessage ()">
11 </form> </div>
12 </body>
13 14
Ø reinput_error.jsp
Select WebRoot-> New-> JSP under the Welcome_SS project, name it reinput_error.jsp, and Finish;
Enter the following code
1 <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
2
3 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
4 5 <body>
6 <div align = "center"> <form name = "form1" method = "post" action = "login. do "> <font color =" # ff0000 "> * incorrect user name or password! </Font> <br>
7 username: <input type = "text" name = "username"> <br>
8 password: <input type = "text" name = "password"> <br>
9 <input type = "submit" name = "submit1" value = "submit">
10 <input type = "reset" name = "submit2" value = "reset" onclick = "deleteAllMessage ()">
11 </form> </div>
12 </body>
13 14
Ø success. jsp
Select WebRoot-> New-> JSP under the Welcome_SS project, name it success. jsp, and Finish;
Enter the following code
1 <% @ page language = "java" pageEncoding = "UTF-8" %>
2
3 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
4 5 6 <! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
7 8 <body>
9 <% = request. getAttribute ("message") %>! <Br>
10. <a href = "login. jsp"> sign in </a> <br>
11. <a href = "index. jsp"> sign out </a>
12 </body>
13 Ø LoginAction. jsp
Select src-> com-> yourcompany-> struts under the Welcome_SS project, New-> files: action, and then in the folder New-> Java, named LoginAction. java, Finish;
Enter the following code
1
2 package com. yourcompany. struts. action;
3
4 import java. util. Locale;
5
6 import javax. servlet. http. HttpServletRequest;
7 import javax. servlet. http. HttpServletResponse;
8 import javax. servlet. http. HttpSession;
9
10 import org. apache. struts. action. Action;
11 import org. apache. struts. action. ActionForm;
12 import org. apache. struts. action. ActionForward;
13 import org. apache. struts. action. ActionMapping;
14 import org. apache. struts. util. MessageResources;
15
16 import com. yourcompany. struts. form. LoginForm;
17
18 public class LoginAction extends Action {
19
20 private String message;
21
22 public String getMessage (){
23 return message;
24}
25
26 public void setMessage (String message ){
27 this. message = message;
28}
29
30 public ActionForward execute (ActionMapping mapping,
31 ActionForm form,
32 HttpServletRequest request,
33 HttpServletResponse response)
34 LoginForm userform = (LoginForm) form;
35 if (userform. getUsername (). equals ("") | (userform. getPassword (). equals (""))){
36 return mapping. findForward ("reinput_blank ");
37}
38 else if (userform. getUsername (). equals ("success") & (userform. getPassword (). equals ("success "))){
39 request. setAttribute ("message", getMessage () + userform. getUsername ());
40 return mapping. findForward ("success ");
41}
42 else {
43 return mapping. findForward ("reinput_error ");
44}
45}
46}
47
Ø LoginForm. jsp
Select src-> com-> yourcompany-> struts under the Welcome_SS project, New-> files: form, and then in the folder New-> Java, named LoginAction. java, Finish;
Enter the following code
1
2 package com. yourcompany. struts. form;
3
4 import javax. servlet. http. HttpServletRequest;
5 import org. apache. struts. action. ActionErrors;
6 import org. apache. struts. action. ActionForm;
7 import org. apache. struts. action. ActionMapping;
8
9 public class LoginForm extends ActionForm {
10
11/** username property */
12 private String username;
13 private String password;
14 public String getPassword (){
15 return password;
16}
17 public void setPassword (String password ){
18 this. password = password;
19}
20 public String getUsername (){
21 return username;
22}
23 public void setUsername (String username ){
24 this. username = username;
25}
26}
5. Write the configuration file (struts-config.xml and applicationContext. xml) according to the code of the previous step)
Choose WebRoot under the Welcome_SS project-> WEB-INF-> struts-config.xml
Enter the following code:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <! DOCTYPE struts-config PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 1.2 //" http://struts.apache.org/dtds/struts-config_1_2.dtd ">
3
4 <struts-config>
5 <data-sources/>
6 <form-beans>
7 <form-bean name = "loginForm" type = "com. yourcompany. struts. form. LoginForm"/>
8 </form-beans>
9
10 <global-exceptions/>
11 <global-forwards/>
12 <action-mappings>
13 <action
14 attribute = "loginForm"
15 input = "/login. jsp"
16 name = "loginForm"
17 path = "/login"
18 scope = "request"
19 type = "org. springframework. web. struts. DelegatingActionProxy">
20 <forward name = "success" path = "/success. jsp"/>
21 <forward name = "reinput_blank" path = "/reinput_blk.jsp"/>
22 <forward name = "reinput_error" path = "/reinput_error.jsp"/>
23 </action>
24
25 </action-mappings>
26
27 <message-resources parameter = "com. yourcompany. struts. ApplicationResources"/>
28
29 <plug-in className = "org. springframework. web. struts. ContextLoaderPlugIn">
30 <set-property = "contextConfigLocation" value = "/WEB-INF/classes/applicationContext. xml"/>
31 </plug-in>
32
33 </struts-config>
34
35
Select src-> applicationContext. xml under the Welcome_SS Project
Enter the following code:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3 <beans>
4 <bean name = "/login" class = "com. yourcompany. struts. action. LoginAction">
5 <property name = "message">
6 <value> Welcome _ </value>
7 </property>
8 </bean>
9 </beans>
10
6. Deploy and start Web Service (Tomcat)
(For details, refer to another article: two deployment methods of Tomcat in Myeclipse)
A. Under apache-tomcat-5.5.20 "conf, locate server. xml, open it, and add a sentence before </Host>.
<Context path = "/wess" debug = "0" docBase = "C: Documents and SettingsttworkspaceWelcome_SSWebRoot" reloadable = "true"> </Context>
Note: C: Documents and SettingsttworkspaceWelcome_SS are the storage path of the project.
B. on the toolbar of Myeclipse, find a button that looks like a chassis. Click it to open the Tomcat server.
7. Run
Enter http: // localhost: 8080/wess In the IE browser to run the program! If the user name and password are both success.