idea 配置一个struts2的简单的helloWorld


1.创建新的项目,打勾如下,选择好tomcat之类,然后jar包是自己弄好的了,也可以自动下载......      


idea自动生成了默认的web.xml,这里不管它了

 

idea也自动创建了index.jsp的首页.....写上基本的信息吧:

 

idea还创建了action管理器struts.xml

 


接着index.jsp的页面,我们创建下一个页面的jsp文件,放在pages下:

input.jsp,实现了输入表格内容 


这里的action请求引导到下一个action请求,保存内容   

 


把上面两个action,就是product-input.action和product-save.action的返回结果在action容器struts.xml里面配置,如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version= "1.0" encoding= "UTF-8" ?>
 
<!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 
<struts>
 
     <!--<constant name= "struts.devMode" value= "true" />-->
     < package name= "default" extends = "struts-default" >
 
         <!--首页请求,name对应index.jsp中的action名字-->
         <action name= "product-input" >
 
             <!--转到input.jsp页面-->
             <result>/WEB-INF/pages/input.jsp</result>
 
         </action>
 
 
         <!--第二个action请求,name对应input.jsp下的action名字
         引用对象以及其方法-->
         <action name= "product-save" class = "beans.Production" method= "save" >
 
             <!--引用方法中返回的detai,对应name,展示到details下-->
             <result name= "detail" >/WEB-INF/pages/details.jsp</result>
 
         </action>
 
     </ package >
</struts>



接着写一个Production的实现类:Production.java


其中save方法返回值给上面的product-save的请求结果并且展示...  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package beans;
 
/**
  * Created by Anonymous on 2016/3/15.
  */
public class Production {
 
     private int proId;
     private String proName;
     private String proDesc;
     private String proPrice;
 
 
     public Production( int proId, String proName, String proDesc, String proPrice) {
         this .proId = proId;
         this .proName = proName;
         this .proDesc = proDesc;
         this .proPrice = proPrice;
     }
 
     public Production() {
     }
 
     @Override
     public String toString() {
         return "Production{" +
             "proId=" + proId +
             ", proName='" + proName + '\ '' +
             ", proDesc='" + proDesc + '\ '' +
             ", proPrice='" + proPrice + '\ '' +
             '}' ;
     }
 
     public int getProId() {
         return proId;
     }
 
     public void setProId( int proId) {
         this .proId = proId;
     }
 
     public String getProName() {
         return proName;
     }
 
     public void setProName( String proName) {
         this .proName = proName;
     }
 
     public String getProDesc() {
         return proDesc;
     }
 
     public void setProDesc( String proDesc) {
         this .proDesc = proDesc;
     }
 
     public String getProPrice() {
         return proPrice;
     }
 
     public void setProPrice( String proPrice) {
         this .proPrice = proPrice;
     }
 
     public String save() {
 
         System.out.println( "result:" + this );
         return "detail" ;
     }
}


配置Tomcat运行这个东东.....

如果没有配置好Tomcat则要设置,如下:


进入到:

在左上角添加你从Tomcat官网下载好的版本,添加在此。并且在idea中设置好默认的浏览器... 

 


确定deployment中已经有东东:


这个东西也就是这里的东东


点击运行:


结果:


点击进入第二个input.jsp


填写好内容,确定,进入第三个页面,detail.jsp

     

   

      


       






免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM