一、struts2简单样例
步骤1:filter拦截请求,为了防止对初始页面拦截,使用/*.action,而不是/*
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*.action</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>
- index.html
- </welcome-file>
- </welcome-file-list>
步骤2:Action处理,可以继承自xwork2的ActionSupport类;也可以不继承,但必须有public String execute() throws Exception方法
- private String username;
- private String password;
- @Override
- public String execute() throws Exception {
- if (username == null || username.equals("")) {
- return ERROR;
- }
- return SUCCESS;
- }
步骤3:在classpath下面(src/main/java或者src/main/resources),配置struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <struts>
- <package name="struts" extends="struts-default" namespace="/">
- <action name="login" class="com.alibaba.struts2.LoginAction">
- <result name="success" type="dispatcher">/success.jsp</result>
- <result name="error" type="dispatcher">/fail.jsp</result>
- </action>
- </package>
- </struts>
步骤4:在页面提交表单给action处理('/'代表从根webapp开始找,否则代表从当前页面所在目录下找),其中.action是从struts.properties中继承而来的(struts.action.extension=action,,)
- <body>
- <form method="get" action="/login.action">
- <table>
- <tr>
- <th><label for="username">用户名</label></th>
- <td><input type="text" id="username" name="username" /></td>
- </tr>
- <tr>
- <th><label for="password">密码</label></th>
- <td><input type="text" id="password" name="password" /></td>
- </tr>
- <tr>
- <th><label for="submit"></label></th>
- <td>
- <button type="submit" id="submit" name="submit">submit</button> <label
- for="reset"></label>
- <button type="reset" id="reset" name="reset">reset</button>
- </td>
- </tr>
- </table>
- </form>
- </body>
步骤5:取得属性
- username:<%=request.getParameter("username") %><br>
- username: ${param.username}
【注意】EL表达式在Servlet2.5中默认禁用,需要开启:<%@ page isELIgnored="false" %>, 更多内容参考:http://tianya23.blog.51cto.com/1081650/678052
【注意】
(1)获取从ActionContext中获取session,而不需要直接使用HttpServletRequest和HttpServletResponse。
- ActionContext context = ActionContext.getContext();
- context.put("k1", "v1");
- Map session = context.getSession();
- session.put("k2", "v2");
二、Spring MVC使用样例
步骤1:添加Spring的相关依赖
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>java.servlet</artifactId>
- <version>2.5</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.webmvc</artifactId>
- <version>2.5.6</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.core</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.beans</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.context</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.context-support</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.web</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.aop</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>jakarta.commons.logging</artifactId>
- <version>1.1.1</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-asm</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>sourceforge.spring.modules.expression</artifactId>
- <version>3.0.5.RELEASE</version>
- </dependency>
步骤2:在web.xml中配置spring mvc的入口
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>*.test</url-pattern>
- </servlet-mapping>
也可以指定spring servlet拦截之后,Spring mvc的配置文件
- <servlet>
- <servlet-name>hello</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/*.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
【注意】
1、在src源下面的文件,都将被映射到/WEB-INF/classes/下面,可以使用通配符。
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!--
- 指定XML文件位置
- <param-value>/WEB-INF/classes/springmvc.xml
- <param-value>classpath*:springmvc.xml
- -->
- <!-- 在classpath路径下去寻找springmvc.xml文件 -->
- <param-value>classpath:springmvc.xml
- </param-value>
- </init-param>
2、<load-on-startup>1</load-on-startup> 让容器启动的时候就加载配置文件
步骤3:如果没有指定spring mvc的映射文件名称,默认使用:在WEB-INF目录下的*-servlet.xml文件,其中*为web.xml中配置的servlet-name。本例为:springmvc,则寻找的配置文件为:springmvc-servlet.xml,里面的配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="start.test">LoginSpringAction</prop>
- </props>
- </property>
- </bean>
- <bean id="LoginSpringAction" class="com.alibaba.spring.LoginSpringAction"></bean>
- <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </beans>
解释:如果start.test访问,则使用com.alibaba.spring.LoginSpringAction进行处理。
处于jsp页面隐藏的考虑,放在/WEB-INF目录下,按照跳转要求,则找/WEB-INF/jsp/start1.jsp页面。其中start1是由Controller继承类中new ModelAndView("start1")决定的。前缀和后缀是在*-servlet.xml中指定的,本例为springmvc-servlet.xml。
【注意】如果需要使用注解annotation的方式,在需要引入context和mvc命名空间,具体如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
- default-autowire="byName">
- </beans>
步骤4:LoginSpringAction.java文件
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.Controller;
- public class LoginSpringAction implements Controller {
- @Override
- public ModelAndView handleRequest(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- System.out.println("username:" + username + ", password:" + password);
- return new ModelAndView("start1");
- }
- }
【注意】使用annotation方式,可以避免耦合方式,也方便单元测试
setp 1:为controller的类添加注解:@Controller
setp 2:为方法添加@RequestMapping
在类级别定义:@RequestMapping("/test"),该类的根级别,可选
在方法上定义:@RequestMapping("/start"),相对与类级别,必选,访问方式:/test/start.do
setp 3:在spring servlet配置文件中添加扫描功能,使annotation产生作用
- <!-- 把标记了@Controller注解的类转换为bean -->
- <context:component-scan base-package="test.controller" />
setp 4:在spring servlet中使用视图解析类,如UrlBasedViewResolver, 在里面配置前缀和后缀,viewClass属性为可选。
步骤5:启动jetty访问,url:http://localhost:8080/start.test, 则后台输出:Controller is working
补充:制作一个index.jsp页面开始运转
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="gbk"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <h2>Hello World!</h2>
- <body>
- <form method="get" action="/start.test">
- <table>
- <tr>
- <th><label for="username">用户名</label>
- </th>
- <td><input type="text" id="username" name="username" />
- </td>
- </tr>
- <tr>
- <th><label for="password">密码</label>
- </th>
- <td><input type="text" id="password" name="password" />
- </td>
- </tr>
- <tr>
- <th><label for="submit"></label>
- </th>
- <td>
- <button type="submit" id="submit" name="submit">submit</button> <label
- for="reset"></label>
- <button type="reset" id="reset" name="reset">reset</button></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
访问:http://localhost:8080/index.jsp
【补充】
(1)对于不需要的controller的login.jsp页面,可以使用spring自带的org.springframework.web.servlet.mvc.ParameterizableViewController,而不需要自己再写controller的实现。具体配置如下:
- <bean id="LoginAction"
- class="org.springframework.web.servlet.mvc.ParameterizableViewController">
- <property name="viewName" value="login"></property>
- </bean>
它会根据策略寻找到/WEB-INF/jsp/login.jsp
(2)如果login.test直接找login.jsp,即login是一致的,不需要改变,则可以更加省略,直接写为:
- <bean id="LoginAction"
- class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
三、Webx MVC(以webx3为基础)
1、webx3的入口点
- <filter>
- <filter-name>webx</filter-name>
- <filter-class>com.alibaba.citrus.webx.servlet.WebxFrameworkFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>webx</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
webx.xml:每个webx应用,都需要有一个webx.xml配置文件。这个文件定义了Webx所用到的所有services的配置。
pipeline.xml:即管道,它是由一个或多个“阀门Valve”构成的。可以看做webx框架的总控文件。
log4j.xml:日志系统配置文件。对于程序的排错至关重要。
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/678001,如需转载请自行联系原作者