我只想2 functions
JAVA Spring 3.0 MVC
page1.htm
和page2.htm
只有CodeIgniter
,只有1个控制器,有点在/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controllers;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
// Import models
import models.LoginModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* @author Candy
*/
@RequestMapping("/app")
public class LoginController extends SimpleFormController {
public LoginController()
{
//Initialize controller properties here or
//in the Web Application Context
setCommandClass(models.Employee.class);
setCommandName("Employee");
//setSuccessView("pages/login/login_form_view");
//setFormView("pages/login/login_execute_view");
}
// On form load
@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
{
LoginModel loginModel = new LoginModel(request);
if(!loginModel.isLogged())
{
ModelAndView mv = new ModelAndView("pages/login/login_form_view");
// mv.addObject("chestie",loginModel.isLogged());
return mv;
}
else
{
return new ModelAndView("redirect:/dashboard.htm");
}
}
@Override
protected void doSubmitAction(Object command) throws Exception
{
throw new UnsupportedOperationException("Not yet implemented");
}
//Use onSubmit instead of doSubmitAction
//when you need access to the Request, Response, or BindException objects
@Override
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception
{
// User has submited a POST request
ModelAndView mv = new ModelAndView("pages/login/login_execute_view");
// Check if the user is logged or not, user must be NOT LOGGED in order to log
LoginModel loginModel = new LoginModel(request);
if(!loginModel.isLogged())
{
// Just continue
String email = request.getParameter("email").toString();
String password = request.getParameter("password").toString();
// Check this email/pass
Map<String,String> resultLogin = loginModel.login(email, password);
String result = resultLogin.get("result").toString();
String reason = resultLogin.get("reason").toString();
// String qqq = resultLogin.get("qqq").toString();
/*
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
*/
if(result == "true")
{
// Login is good, redirect to dashboard
return new ModelAndView("redirect:/dashboard.htm");
// Display the error
//mv.addObject("loginResult",result);
//mv.addObject("loginReason",reason);
//mv.addObject("qqq",qqq);
//mv.addObject("qqq2",request.getSession().getAttribute("is_logged").toString());
}
else
{
// Display the error
mv.addObject("loginResult",result);
mv.addObject("loginReason",reason);
return mv;
}
}
else
{
return new ModelAndView("redirect:/login.htm");
}
}
}
,我不知道为什么这么难......
这就是我现在所做的事情
applicationContext.xml
<bean class="controllers.LoginController"/>
dispatcher-servlet.xml
<bean class="controllers.LoginController" p:formView="pages/login/login_form_view" p:successView="pages/login/login_execute_view"/>
web.xml
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>controllers.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
如果我删除
,此控制器将无效logout()
为什么我在创建新控制器时需要编辑3个XML?是不是更简单的方式?
我现在想要的只是创建另一个加载ModelAndView mv = new ModelAndView("pages/login/logout_view");
的函数@RequestMapping("/app")
@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
{
ModelAndView mv = new ModelAndView("pages/login/login_form_view");
return mv;
}
,这就是全部。
这个
SimpleFormController
给了我404错误......
extend Controller
,AbstractController
,NETBEANS
(我正在使用CodeIgniter
)。我很困惑,我希望有一个像apache rewrite
这样的好的Java框架。我不太明白映射是如何工作的,但我确实知道{{1}}是如何工作的。非常感谢。
答案 0 :(得分:1)
如果您使用的是Spring 3,则可以轻松地为控制器类使用@controller
注释,这些注释用于页面请求和导航。
您可以对业务逻辑使用@Service
注释,为数据库事务使用@Repository
,并使用@Autowired
注释将这些图层绑定在一起。
所以您的请求就是这样(UI Request
- &gt; @Controller
- &gt; @Service
- &gt; @Repository
)并再次回到原来的控制器类导航
但是关于你的问题,当你在春季用@Controller
注释标记一个类时,这只是意味着你选择了这个类的URL映射和导航,下面的示例展示了如何使用@RequestMapping
用于URL映射的Controlller类内部的注释:
@Controller
@RequestMapping("/employee")
public class Employees{
@AutoWired
private EmployeeServiceInterface empServInt;
@ModelAttribute("empAdd")
public Employee createEmployeeModel(){
return new Employee();
}
@RequestMapping(value="/add", method = RequestMethod.POST)
public String addEmployee(@ModelAttribute("empAdd")Employee employee,Model model, HttpSesion session){
Employee employee = empServInt.createUser(employee);
if(yourCondition()){
return "redirect:/TO_THIS_PAGE";
}
return "GO_TO_PAGE_NAVIGATION_HERE";
}
}
请注意,由于HOST_NAME:PORT/PROJECT_NAME/employee/add
注释值,当请求附带网址@RequesMapping
时会激活@RequestMapping。
您在示例中看到的@ModelAttribute
为您的页面创建了一个模型,可帮助您使用Spring Form标记中的commandName属性从UI提交Employee Object Bean值,该标记使用{将html输入绑定到Employee Bean类{1}}属性:
path=""
第三,请注意@AutoWired可以自动将接口连接到实现,所以在我的示例中,如果像EmployeeServiceImpl这样的类通过调用接口方法实现接口<form:form commandName="empAdd" method="post" >...</form:form>
:EmployeeServiceInterface
spring自动调用相关的EmployeeServiceImpl.createUser(employee)实现方法。
第四:请注意,您可以使用Controller和Service&amp;存储库注释工作,您应该在Spring Descriptor XML文件中定义具有这些注释的包,如下所示:
empServInt.createUser(employee);
第五,如果您注意到Employees中的方法具有提供导航的String返回类型,但为了使其工作,您应该定义将在Spring Descriptor XMl文件中返回的页面前缀和后缀,如下所示:< / p>
<context:component-scan base-package="java.package" />
根据上面的配置,如果使用@RequestMapping注释的方法返回<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
String,则Spring从此位置获取页面:
[PREFIX]员工[POSSIX] - &gt; /WEB-INF/view/employee.jsp
为了更好地理解,我找到了一个有用的示例链接。 Link
祝你好运!