Spring portlet Demo

     

 Spring portlet 的配置过程:

 1、 web.xml  配置

<servlet>
   <servlet-name>view-servlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>view-servlet</servlet-name>
   <url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping> 

  

2、*-portlet.xml 的配置

 <bean class="net.ueye.test.DemoController"/>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/"/>
     <property name="suffix" value=".jsp"/>
 </bean>
 <context:annotation-config/> 
  

3、 DemoController

@Controller
@RequestMapping("view")
public class DemoController {
    
    @RequestMapping
    public String index(Model model){        
        model.addAttribute("accounts",new AccountService().findAll());
        return "index";
    }
    
    @RequestMapping(params="action=edit")
    public String edit(@RequestParam("id")String id,Model model){
        model.addAttribute("account",new AccountService().get(id));
        return "edit";
    }
    
    @RequestMapping(params="action=add")
    public String newAccount(){        
        return "add";
    }
    
    @RequestMapping(params="action=create")
    public void create(ActionResponse response,@ModelAttribute("acc")Account acc,Model model){
        AccountService as=new AccountService();
        as.insert(acc);
        model.addAttribute("accounts",as.findAll());
        response.setRenderParameter("action","index");
    }
    
    @RequestMapping(params="action=update")
    public void update(ActionRequest request,ActionResponse response,Model model){
        AccountService as=new AccountService();
        as.get(request.getParameter("id")).setUsername(request.getParameter("username"));
        model.addAttribute("accounts",as.findAll());
        response.setRenderParameter("action","index");
    }
    
    @RequestMapping(params="action=delete")
    public String delete(@RequestParam("id")String id,Model model){
        AccountService as=new AccountService();
        as.remove(id);
        model.addAttribute("accounts",as.findAll());
        return "index";
    }
    
} 
 

4、 index.jsp

 <table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th colspan="2">Actions</th>
    </tr>
    <c:forEach var="account" items="${accounts }">
    <tr>
        <td>${account.id }</td>
        <td>${account.username }</td>
        <td><a href="<portlet:renderURL>
                    <portlet:param name="action" value="edit"/>
                    <portlet:param name="id" value="${account.id }"/>
               </portlet:renderURL>">Edit</a>
        </td>
        <td><a href="<portlet:renderURL>
                     <portlet:param name="action" value="delete"/>
                     <portlet:param name="id" value="${account.id }"/>
                 </portlet:renderURL>">Delete</a>
        </td>
    </tr>
    </c:forEach>
    <tr>
       <td colspan="4">
           <a href="<portlet:renderURL>
                       <portlet:param name="action" value="add"/>
                       <portlet:param name="id" value="${account.id }"/>
                    </portlet:renderURL>">Add</a>
       </td>
    </tr>
 </table> 
 

5、add.jsp

<form action="<portlet:actionURL/>" method="post">
   <input type="hidden" name="action" value="create"/>
   <table>
      <tr>
        <th>ID</th>
        <td><input type="text" name="id" value="${account.id }" /></td>
      </tr>
      <tr>
        <th>Name</th>
        <th><input type="text" name="username"/></th>
      </tr>
      <tr>
        <td colspan="2">
           <input type="submit" value="submit"/>
           <input type="reset" value="reset"/>
        </td>
       </tr>
    </table>
</form> 
 
       

/Files/rubys/demo.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值