《Java从入门到放弃》入门篇:Struts2的基本访问方式

Struts2是个什么玩意呢?

引用百度百科的介绍:Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互

介绍完毕···

 

其核心原理图网上很多,我这儿做了一个简单的修改,如果有说明得不够恰当的地方,请指正。原理图请Look:

wKiom1l4U9yTE1zLAAKXjfqcBXE049.png-wh_50

 

好了,到这儿差不多闲扯完了,来聊点有用的。Struts2到底怎么用呢,看下面的步骤

1.新建Web项目(好像是废话)

2.导入Struts2的Jar包

3.编写web.xml配置

4.编写struts.xml配置

5.编写Action类

6.编写JSP页面

 

前两步忽略,直接从第三步开始:

3.在web.xml引入Struts2(其实就是个Filter)

1
2
3
4
5
6
7
8
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 

4.编写struts.xml配置(该文件放到src根目录中)

1
2
3
4
5
6
<package name="default" namespace="/" extends="struts-default">
    <!-- 指定Method调用 -->
    <action name="userLogin" class="com.pxy.action.Hello">
    <result>/login.jsp</result>
    </action>
</package>

 

5.编写Action类(放在com.pxy.action包中)

1
2
3
4
5
6
7
8
public class Hello extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("默认调用的方法!");
        return SUCCESS;
    }
 
}

 

6.编写JSP页面

1
2
3
4
5
6
7
8
<body>
      登录界面<br />
    <form action="" method="post">
        账号:<input type="text" name="loginid" /><br />
        密码:<input type="password" name="loginpwd" /><br />
        <input type="submit" value="登录" />
    </form>
  </body>

 

到这儿所有的编码工作完成,接下来我们在地址栏输入http://localhost:8080/strDemo/userLogin.action试试效果。正常访问到了login.jsp页面。

wKioL1l4V4aB__yiAAAex-mtWfc416.png-wh_50

我修改了Tomcat的端口为8888

各位客官还满意吧。

如果出错了,那肯定是你的姿势不对,起来重睡就好了。

如果还是不对,那就多睡一会儿,睡醒后肯定就正常了,请相信我。。。

posted @ 2017-08-01 15:42  软件思维  阅读(309)  评论(0编辑  收藏  举报