首页 > 代码库 > SSH三大框架注解整合(一)

SSH三大框架注解整合(一)

1.导入jar包,ssh的jar包一共是38个,此时还需要多加一个包,就是struts的注解插件jar。

2.在web.xml文件中配置struts filter 和spring 的listener。代码如下:

   <!-- spring 监听器 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param> 
  <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>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

3.编写jsp页面

<s:form action="addBook" namespace="/" method="post" theme="simple">
 书名 <s:textfield name="name"/> <br/>
 作者 <s:textfield name="author" /> <br/>
 <s:submit value="http://www.mamicode.com/添加图书"></s:submit>
</s:form>

4.利用struts注解编写action

@ParentPackage(value = "http://www.mamicode.com/struts-default")
@Namespace("/")
public class BookAction extends ActionSupport implements ModelDriven<Book>{
 
 //模型驱动
 public Book book = new Book();
 public void setBook(Book book) {
  this.book = book;
 }

 @Override
 public Book getModel() {
  return book;
 }
 
 @Override
 @Action(value="http://www.mamicode.com/addBook",results={@Result(name="success",location="/success.jsp")})
 public String execute() throws Exception {
  
  return SUCCESS;
 }

}